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:  

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  
   String one;
   String 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:


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:


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  
   Collection<JsonBean> mArray;

and also write the code in your function.

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

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

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:  

In functions:

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

Also you can use in class like that

public class jsonarray  
   Collection<Integer> values;

Using Gson to decode the dynamic key

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


The function:

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

In class

public class Array  
   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.