Processing of Java JSON

Source: Internet
Author: User
Tags tojson

Maven Dependency

<dependencies>        <dependency>    <groupId>com.alibaba</groupId>    <artifactid >fastjson</artifactId>    <version>1.2.5</version></dependency>    </ Dependencies>

Read JSON

classreadjson{ Public Static voidMain (string[] args) {String jsonstr= "{\" one\ ": \" one\ ", \" two\ ": [{\" two_1_1\ ": 2.11, \" two_1_2\ ": 2.12}, {\" two_2_1\ ": \" 2.21\ "}], \" three\ ": [\" Abc\ ", FALSE], \ "four\": {\ "four_1\": 4.1, \ "four_2\": 4.2}} "; //One : Simple Type//One: Object array (most complex)//three: Array type//Four : Object TypesJsongoogle (JSONSTR);    Jsonalibaba (JSONSTR); }    //gosn Read processing JSON     Public Static voidJsongoogle (String jsonstr) {jsonparser parser=NewJsonparser (); Jsonobject Jsonobj=(Jsonobject) parser.parse (JSONSTR); String One= Jsonobj.get ("One"). getasstring (); System.out.println (one);// OneJsonarray Twoobjarray= Jsonobj.get ("the"). Getasjsonarray (); System.out.println (Twoobjarray);//[{"Two_1_1": 2.11, "two_1_2": 2.12},{"two_2_1": "2.21"}]Jsonobject twoobj = (jsonobject) twoobjarray.get (0); String of= Twoobj.get ("Two_1_1"). Getasstring ();//can be treated as StringSystem.out.println (both);//2.11Jsonarray Threearray= Jsonobj.get ("three"). Getasjsonarray (); String Three_1= Threearray.get (0). getasstring (); Booleanthree_2 = Threearray.get (1). Getasboolean (); System.out.println (Three_1+ three_2);//AbcfalseJsonobject fourobj= Jsonobj.get ("Four"). Getasjsonobject (); Doublefour_1 = Fourobj.get ("four_1"). getasdouble (); System.out.println (four_1);//4.1    }    //Fastjson Read processing JSON     Public Static voidJsonalibaba (String jsonstr) {jsonobject jsonobj=Json.parseobject (JSONSTR); String One= Jsonobj.getstring ("One"); System.out.println (one);// OneJsonarray Twoobjarray= Jsonobj.getjsonarray ("the"); System.out.println (Twoobjarray);//[{"Two_1_1": 2.11, "two_1_2": 2.12},{"two_2_1": "2.21"}]Jsonobject twoobj = twoobjarray.getjsonobject (1); String two_2= Twoobj.getstring ("Two_2_1"); System.out.println (two_2);//2.21Jsonarray Threearray= Jsonobj.getjsonarray ("three"); String Three_1= threearray.getstring (0); Booleanthree_2 = Threearray.getboolean (1); System.out.println (Three_1+ three_2);//AbcfalseJsonobject fourobj= Jsonobj.getjsonobject ("Four"); String four_1= Fourobj.getstring ("four_1"); System.out.println (four_1);//4.1    }}

Write JSON

 Public class person{    private  String name;     Private int Age ;     Private Double salary;     Private Boolean Hasbaby;     Private List<string> babynames;     // setter/getter/tostring etc }
 Public classwritejson{ Public Static voidMain (string[] args) {writejsongoogle ();    Writejsonalibaba (); }    //Gson Writing JSON     Public Static voidWritejsongoogle () {jsonobject jsonobj=NewJsonobject (); Jsonobj.addproperty ("One", "Onestr"); Jsonobj.addproperty ("The Other",false); Jsonobject Threeobj=NewJsonobject (); Threeobj.addproperty ("Three", 3); Jsonobj.add ("Three", Threeobj); Jsonarray Jsonarray=NewJsonarray (); Jsonobject four_1=NewJsonobject (); Four_1.addproperty ("Four_1", 4.1); Jsonobject four_2=NewJsonobject (); Four_2.addproperty ("Four_2",true);        Jsonarray.add (four_1);        Jsonarray.add (four_2); Jsonobj.add ("Four", Jsonarray);        System.out.println (Jsonobj.tostring ()); //{"One": "Onestr", "one": false, "three": {"three": 3}, "four": [{"four_1": 4.1},{"four_2": True}]}    }    //Fastjson Writing JSON     Public Static voidWritejsonalibaba () {Map<string, object> jsonmap =NewTreemap<string, object>(); Jsonmap.put ("One", "Onestr"); Jsonmap.put ("The Other",false); Map<string, object> threeobj =NewHashmap<string, object>(); Threeobj.put ("Three_1", "3.1"); Threeobj.put ("Three_2", 3.2); Jsonmap.put ("Three", Threeobj); Jsonobject Jsonobj=NewJsonobject (JSONMAP); List<String> Babynames =NewArraylist<string>(); Babynames.add ("ABC"); Babynames.add ("Def"); Person Person=NewPerson ("Gusi", 12, 7000.00,true, Babynames); Jsonobj.put ("Four", person); Jsonobj.put ("Five", 5);        System.out.println (Jsonobj.tojsonstring ()); //{"Five": 5, "four": {"Age": "Babynames": ["abc", "Def"], "Hasbaby": True, "name": "Gusi", "Salary": 7000}, "one": " Onestr "," three ": {" Three_1 ":" 3.1 "," Three_2 ": 3.2}," both ": false}    }}

Common conversions for object types and JSON

 Public classDemoImplementsserializable{String name; intAge ; BooleanMan ;  PublicDemo () {Super(); }     PublicDemo (String name,intAgeBooleanMans) {        Super();  This. Name =name;  This. Age =Age ;  This. Man =Man ; }    //Setter/getter, must not forget, otherwise fastjson cannot set the value@Override PublicString toString () {return"Demo [name=" + name + ", age=" + Age + ", man=" + man + "]"; }}
//GsonDemo Demo1 =NewDemo ("A", 1,false); String Json1=NewGson (). ToJson (DEMO1);//JavaBean to StringSystem.out.println (json1);D emo demo2=NewGson (). Fromjson (Json1, Demo.class);//string to JavaBeanSystem.out.println (DEMO2); Jsonobject jsonObj1= (Jsonobject)NewJsonparser (). Parse (Json1);//string to JsonobjectSystem.out.println (JSONOBJ1); String Json2= Jsonobj1.tostring ();//Jsonobject to StringSystem.out.println (json2);//FastjsonDemo Demo3 =NewDemo ("B", 2,true); String Json3= Json.tojsonstring (DEMO3);//JavaBean to StringSystem.out.println (json3);D emo Demo4= Json.parseobject (Json3, Demo.class);//string to JavaBeanSystem.out.println (DEMO4); Jsonobject JsonObj2= Json.parseobject (Json3);//string to JsonobjectSystem.out.println (JSONOBJ2); String Json4= Jsonobj2.tojsonstring ();//Jsonobject to StringSystem.out.println (JSON4); Jsonobject jsonObj3= (jsonobject) Json.tojson (DEMO3);//JavaBean to JsonobjectSystem.out.println (JSONOBJ3);

Processing of Java JSON

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.