Examples of how JSON is used

Source: Internet
Author: User

Example 1:

1 ImportNet.sf.json.JSONArray;2 ImportNet.sf.json.JSONObject;3 4 /**5 * Created by Leiqiang on 2014/7/25.6  */7  Public classJsonobjectsample {8     //Create a Jsonobject object9     Private Staticjsonobject Createjsonobject () {TenJsonobject Jsonobject =NewJsonobject (); OneJsonobject.put ("username", "Leiqiang"); AJsonobject.put ("Sex", "male"); -Jsonobject.put ("nickname", "Lqproplus"); -         returnJsonobject; the     } -  -      Public Static voidMain (string[] args) { -Jsonobject jsonobject = Jsonobjectsample.createjsonobject ();//static method, directly through the class name + method call +         //Output Jsonobject Object -System.out.println ("Jsonobject:" +jsonobject); +  A         //interpreting the type of the output object at         BooleanIsArray =Jsonobject.isarray (); -         BooleanIsEmpty =jsonobject.isempty (); -         BooleanIsnullobject =Jsonobject.isnullobject (); -System.out.println ("is an array:" + IsArray + ", is empty:" +IsEmpty -+ ", Isnullobject:" +isnullobject); -  in         //adds an attribute, appending an element after jsonobject.  -Jsonobject.element ("Address", "Beijing"); toSystem.out.println ("Object after adding a property:" +jsonobject); +  -         //returns a Jsonarray object theJsonarray Jsonarray =NewJsonarray (); *Jsonarray.add (0, "This is a Jsonarray value"); $Jsonarray.add (1, "another Jsonarray value");Panax NotoginsengJsonobject.element ("Jsonarray", Jsonarray); -         //Add a jsonarray behind the Jsonobject . theJsonarray array = Jsonobject.getjsonarray ("Jsonarray"); +         //System.out.println ("Object after joining Jsonarray:" +jsonobject); A  the  +System.out.println ("Returns a Jsonarray object:" +array); -         //value after adding Jsonarray $SYSTEM.OUT.PRINTLN ("result =" +jsonobject); $  -         //returns a string based on key -String username = jsonobject.getstring ("username"); theSystem.out.println ("username=" +username); - Wuyi         //convert characters to Jsonobject theString temp =jsonobject.tostring (); -Jsonobject Object =Jsonobject.fromobject (temp); Wu  -         //return value after conversion based on key AboutSystem.out.println ("nickname=" + object.get ("nickname"))); $  -     } -}

Operation Result:

1jsonobject:{"username": "Leiqiang", "Sex": "Male", "nickname": "Lqproplus"}2is an array:false, whether it is empty:false, Isnullobject:false3Object after adding the attribute: {"username": "Leiqiang", "Sex": "Male", "nickname": "Lqproplus", "Address": "Beijing"}4Returns a Jsonarray object: ["This is a Jsonarray value", "another Jsonarray value")]5Results ={"username": "Leiqiang", "Sex": "Male", "nickname": "Lqproplus", "Address": "Beijing", "Jsonarray": ["This is a Jsonarray value "," another Jsonarray value "]}6Username==>Leiqiang7Nickname=lqproplus

Example 2:

1 ImportNet.sf.json.JSONArray;2 ImportNet.sf.json.JSONObject;3 4 /**5 * Created by Leiqiang on 2014/7/25.6  */7  Public classJsontest {8      Public Static voidMain (String args[]) {9Jsonobject jsonObj0 =NewJsonobject ();TenJsonobject jsonObj1 =NewJsonobject (); OneJsonobject JsonObj2 =NewJsonobject (); AJsonobject jsonObj3 =NewJsonobject (); -Jsonarray Jsonarray =NewJsonarray (); -  the         //Create JsonObj0 -Jsonobj0.put ("NAME0", "Zhangsan"); -Jsonobj0.put ("Sex1", "female"); -System.out.println ("JsonObj0:" +jsonObj0); +  -         //Create JsonObj1 +Jsonobj1.put ("name", "Lisi"); AJsonobj1.put ("Sex", "male"); atSystem.out.println ("Jsonobj:" +jsonObj1); -  -         //Create a jsonObj2 with two entries with entries of Jsonobj0,jsonobj1 -Jsonobj2.put ("Item0", jsonObj0); -Jsonobj2.put ("Item1", jsonObj1); -System.out.println ("JsonObj2:" +jsonObj2); in  -         //Create jsonObj3 with only one entry with content JsonObj2 toJsonobj3.element ("J3", jsonObj2); +System.out.println ("JsonObj3:" +jsonObj3); -  the         //Add the Jsonobject object to the Jsonarray. The difference between Jsonarray and Jsonobject is that Jsonarray is more than jsonobject square brackets [] * Jsonarray.add (jsonObj1); $System.out.println ("Jsonarray:" +Jsonarray);Panax Notoginseng  -Jsonobject JsonObj4 =NewJsonobject (); theJsonobj4.element ("Weather", Jsonarray); +System.out.println ("JsonObj4:" +jsonObj4); A     } the}

Operation Result:

1 jsonobj0:{"NAME0": "Zhangsan", "sex1": "Female"}2 jsonobj:{"name": "Lisi", "Sex": "Male" }3 jsonobj2:{"item0": {"NAME0": "Zhangsan", "sex1": "Female"}, "Item1": {"name": "Lisi", "Sex": "Male" }}4 jsonobj3:{"J3": {"item0": {"NAME0": "Zhangsan", "sex1": "Female"}, "Item1": {"name": "Lisi", "sex" : "Male"}}}5 jsonarray:[{"name": "Lisi", "Sex": "Male"}]6 jsonobj4:{"Weather": [{"Name": "Lisi", "Sex": "Male"}]}

Example 3:

1 ImportNet.sf.json.JSONObject;2 3 /**4 * Created by Leiqiang on 2014/7/25.5  */6  Public classJsonfromobject {7      Public Static voidMain (string[] args) {8 9Jsonobject object = Jsonobject.fromobject ("{\" username\ ": \" leiqiang\ ", \" sex\ ": \" male \ ", \" nickname\ ": \" Lqproplus\ "}");TenSystem.out.println ("nickname=" + object.get ("nickname"))); One  ASystem.out.println (object.getstring ("username")); -  -Jsonobject Object1 =NewJsonobject (); theObject1.put ("a", object); - System.out.println (object1); -     } -  +}

Operation Result:

1 nickname=lqproplus2leiqiang3 {"A": {"username": "Leiqiang", " Sex ":" Male "," nickname ":" Lqproplus "}}

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.