Android Gson
The current client has to interact with the server, and the format of the data is basically JSON, so in the Android development is often used in JSON parsing, it is convenient that Google has provided us with a great JSON parsing library –gson, So today, to summarize the various uses of Gson.
Gson's official: Google-gson
Single Object
First, let's look at one of the simplest uses, assuming the JSON data format is this:
{ "id": 100, "body": "It is my post", "number": 0.13, "created_at": "2014-05-22 19:12:38"}
Then we just need to define a corresponding class:
public class Foo { public int id; public String body; public float number; public String created_at;}
Here are just a few lines of code to use:
public static final String JSON_DATA = "...";Foo foo = new Gson().fromJson(JSON, Foo.class);
Here is the simplest use, Created_at directly defines the string type, if you want the date type can also become the following example:
PublicClassFoo{PublicIntId;PublicStringBody;PublicFloatNumber;PublicDateCreated_at;}PublicStaticFinalStringJson_data= "..." ;gsonbuilder = new gsonbuilder (); gsonbuilder. Setdateformat (gson = gsonbuilder. Createfoo = gson. Fromjson (json_datafoo.class
Some people say that Created_at is not Java style, Java Programming specification is the hump structure, then Ok,gson is very human also provides the way of annotations, only need to change the Foo object so that OK:
public class Foo { public int id; public String body; public float number; @SerializedName("created_at") public String createdAt;}
Then the usage does not change, is not very convenient.
Nesting of objects
Suppose you want to return the following data:
{ "id": 100, "body": "It is my post", "number": 0.13, "created_at": "2014-05-22 19:12:38" "foo2": { "id": 200, "name": "haha" }}
So the definition of the object is like this.
PublicClassFoo{public int idpublic string bodypublic float numberpublic string created_atpublic childfoo foo2public class childfoo {public span class= "n" >int idpublic string name}} /span>
Object array
If the JSON array is returned, the following:
[{"id":100 "body" : "It is my Post1" "number" : 0.: "2014-05-20 19:12:38" },{ "id" : 101 "body" : "It is my Post2" : 0. 14: "2014-05-22 19:12:38" } /span>
There are two ways to resolve this:
PublicStaticFinalStringJson_data= "..." ;[] foos = new gson () . Fromjson (json_datafoo[].< span class= "n" >class); // at this time want to turn list// Span class= "No" >list<foo> fooslist = arrays. Aslist (foos
PublicStaticFinalString json_data = "..."; Type ListType = new typetoken<ArrayList<Foo>>() {}. GetType(); ArrayList<Foo> foos = new gson(). Fromjson(json_data, listtype);
Summarize
The above basic summary to the development of commonly used in the centralized type, usage is very simple and convenient, the main need to abstract JSON data into the corresponding data model is OK. But Ali also has a set of his own open-source JSON parsing library –fastjson, is said to be better performance, but the actual application of the sense of Gson resolution has been quite fast, and more accustomed to use Google official things, so the Fastjson did not how to study, later have time to use experience.