Use of "Turn" Android Gson

Source: Internet
Author: User

Android Gson
    • May 22, 2014
    • Android

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:

    • 1, analytic array
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         
    • 2. Parse into List
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.