One minute to learn to use Gson

Source: Internet
Author: User
Tags tojson

Gson is an extremely useful tool for converting json-objects to each other. If you have not, you can go here to download Gson2.2.2.jar

First, prepare two classes, as follows:

public class One {    int integer = ten;    String a = null;   Comtwo = new ();   @Override public   String toString () {       return "integer:" + integer + "  ; A: "+ A +"  ;  " +comtwo.tostring ();   }} public static class of {        int twoint;        String Twostr;        @Override public        String toString () {            //TODO auto-generated method stub            return "Twoint:" + twoint + "
   
    ; TWOSTR: "+ twostr;        }    }
   

New one object to convert it to JSON:

<span style= "color: #006600;" >gson Gson = new Gson ();        One one = new one ();        String onejstr = Gson.tojson (one);        System.out.println (ONEJSTR);</span>

The result is:

<span style= "color: #006600;" >{"integer": Ten, "Comtwo": {"Twoint":0}}</span>

add a value to variable a:

        Gson Gson = new Gson ();        One one = new one ();        One.a = "Zhang Ting";        String onejstr = Gson.tojson (one);        System.out.println (ONEJSTR);

The result is:

<span style= "color: #990000;" >{"integer": Ten, "a": "Zhang Ting", "Comtwo": {"Twoint":0}}</span>

To construct the one object from JSON data:

        String js = "{\" Integer\ ": 10,\" comtwo\ ": {\" twoint\ ":"}} ";        One oneobj = Gson.fromjson (js, one.class);        System.out.println (Oneobj);
The result is:

<span style= "color: #006600;" >integer:10  ; a:null  ;  twoint:0  ; twostr:null</span>

A little more complicated, add two classes:

public static class Onechild extends one{        String three = "I ' m three";        list<string> list;        List<compobj> oblist;        Public Onechild () {            list = new arraylist<string> ();            List.add ("XXX");            List.add ("ting");            Oblist = new arraylist<compobj> ();            Compobj D1 =  new Compobj ();            D1.data= "good";            Compobj D2 =  new Compobj ();            D2.data = "Zhang";            Oblist.add (d1);            Oblist.add (D2);        }    }
public static class compobj{        String data = "Comp obj";            }

To construct a Onechild object:

Onechild child = new Onechild ();        String thjstr = Gson.tojson (child);        System.out.println (THJSTR);

The output is:

<span style= "color: #006600;" >{"three": "I\u0027m three", "List": ["XXX", "Ting"], "oblist": [{"Data": "Good"},{"data": "Zhang"}], "integer": 10, " Comtwo ": {" Twoint ":0}}</span>

construct list (array):

Type ListType = new typetoken<list<string>> () {}.gettype ();        list<string> target = new linkedlist<string> ();        Target.add ("blah");        Target.add ("Gao");        Target.add ("Zhang Ting");//        Gson Gson = new Gson ();        String JSON = Gson.tojson (target, listtype);        SYSTEM.OUT.PRINTLN (JSON);

The result is:

<span style= "color: #990000;" >["blah", "Gao", "Zhang Ting"]</span>

To this gson you learned, the following summarizes the use of Gson, need to pay attention to the content:

1. The key in the JSON data is identical to the variable name in the Java object (case-sensitive)

2. Only Java objects that have values (which are the default values) will be converted into JSON data.

3. Variables in Java objects do not have to correspond to key one by one in JSON data.

4. Variables not found in subclasses are automatically searched for in the parent class.

One minute to learn to use Gson

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.