Use Gson to Parse Object

很早的博文.想着自己还有可能要看.就迁移过来了

It is not feel good if you use the origin Android Json parser.
I bet you that when you use the Android origin Json decode,you will write the A lot of reflection to deal with the json data.
So,We decide to use Gson to help me decode the json code;

Gson Introduce

Google Gson OfficeSite
Gsons User Guide

You should check the website first

Using Gson to Parser the Normal Class

json code:  
{
  "title":"title1",
  "value1":"value1",
  "value2":2
}

The most simple data struct of the Json.let's write the gson wrap class.

public class JsonBean  
{   
   String title;    
   String value1;   
   int value2;   
}

using it in your function

String jsonarray-->jsoncode  
Gson mgson=new Gson();  
JsonBean mBean=mgson.formJson(jsonarray,JsonBean.class);  

Also,We could add java anotation @SerializedName("title") to change the class and json reflection.
the class will like that:

public class JsonBean  
{
  @SerializedName("title")
   String one;
   @SerializedName("value1")
   String value2;
   @SerializedName("value2")
   int value;
}

It is the most easy json to decode.

Note1: if you don`t want to parser some of the key,you can drop it.
Just like:

{jsonarray:[
      {title:"title1",value:"value1",name:"name"}
      {title:"title2",value:"value2",name:"name2"}
      ]}

Note2: If you write the wrong member Name or serizalizedName ,the Gson decode will return the Null values,won`t thrown the Exception.
Make sure about that.

Using gson to parse the json array

Json Array get the two ways to parse.

This is the Json Array Code:
Json Array:

{jsonarray:[
      {title:"title1",value:"value1",name:"name"}
      {title:"title2",value:"value2",name:"name2"}
      ]}

Using the gson type to decode the json array

We can write the class like that:

public class JsonBean  
{
   String title;
   String value;
   String name;
}

using in your code

Stringjsonarray->Json Array  
Type listtype=new TypeToken<List<JsonBean>>().getType();  
Gson mgosn=new Gson();  
List<JsonBean> mBean=gson.fromJson(Stringjsonarray,listtype);  

This is the most convenient way to decode json array to Java List.
also there is another way to Parser the Json Decode

Using the Collection to Decode the Json Array

the Class Like that:

public class JsonArray  
{
   @SerializedName("jsonarray")
   Collection<JsonBean> mArray;
}

and also write the code in your function.

String jsonarray->Json Array  
Gson gson=new Gson();

//way1
//Type Type=new TypeToken<JsonBean>(){}.getType();
//JsonArray marray=gson.fromJson(jsonarray,type);

//way2
JsonArray marray=gson.fromJson(jsonarray,JsonArray.class)  

This is the best two way to decode json array.

Using gson to deocde num arrays

Also you can change the JsonBean to Double to deal with json number array like [1,0,1,2,1.......]
MeanWhile you can use the Collection<Integer> to deal with json number array likec[1,0,1,2,1......]

Json String:  
{"values":[1,2,3,4,5,6,6]}

In functions:

List<Integer> mInt=new ArrayList<Integer>();  
Type mtype=new TypeToken<List<Integer>>(){}.getType();  
mInt=gson.fromJson(string,mtype);  

Also you can use in class like that

public class jsonarray  
{
   @SerializedName("values")
   Collection<Integer> values;
}

Using Gson to decode the dynamic key

Using Map<Keytype,ValueType> to fix this problem.
Json string:

{
item:{  
1:"a",  
2:"b",  
     }}

The function:

String mString--->JsonString  
Map<Integer,String> mMap=null;  
Type mtype=new TypeToken<Map<Integer,String>>(){}.getType();  
mMap=gson.fromJson(mString,mtype);  

In class

public class Array  
{
   @SerializedName("item")
   public Map<Integer,String> item;
   .....
}

The Collection Convert to List:

Collection<Integer> mvalue<---gson.fromJson(......)  
Integer[] mIntValue=(Integer[])mvalue.toArray(new Integer[mvalue.size()]);  
List<Integer> mListValue=Arrays.asList<mIntValue>();  

The End

This is about what I learn from gson,normally it can solve the all gson decode problem.
If you want to help me to improve my blog or find some mistake I made,Plz to Connect me.
Thanks.

friddle

继续阅读此作者的更多文章