1.JAR Package Introduction
To enable the program to run, the Json-lib package must be introduced, and the Json-lib package relies on the following jar packages:
- Commons-lang.jar
- Commons-beanutils.jar
- Commons-collections.jar
- Commons-logging.jar
- Ezmorph.jar
- Json-lib-2.2.2-jdk15.jar
2.JSONObject Object Use
The Json-lib package is a Beans,collections,maps,java arrays and XML and JSON transforms each other's packages. In this example, we will use the Jsonobject class to create the Jsonobject object, and then we print the values of those objects. In order to use the Jsonobject object, we will introduce the "Net.sf.json" package. To add elements to an object, we use the put () method.
2.1. Example 1
Package Jsontest;import Net.sf.json.jsonarray;import Net.sf.json.jsonobject;public class Jsonobjectsample {//Create Jsono Bject object private static Jsonobject Createjsonobject () {Jsonobject jsonobject = new Jsonobject (); Jsonobject.put ("username", "Huangwuyi"); Jsonobject.put ("Sex", "male"); Jsonobject.put ("QQ", "413425430"); Jsonobject.put ("Min.score", New Integer (99)); Jsonobject.put ("nickname", "Mood in the Dream"); return jsonobject; } public static void Main (string[] args) {Jsonobject jsonobject = Jsonobjectsample.createjsonobject ();//static method, Direct Through the class name + method call//Output Jsonobject object System.out.println ("Jsonobject:" + jsonobject); The type of the interpretation output object, Boolean IsArray = Jsonobject.isarray (); Boolean isEmpty = Jsonobject.isempty (); Boolean isnullobject = Jsonobject.isnullobject (); System.out.println ("is an array:" + IsArray + ", whether it is empty:" + IsEmpty + ", Isnullobject:" + isnullobject); Add properties, in JSONAppends an element to an object. Jsonobject.element ("Address", "Xiamen, Fujian Province"); System.out.println ("Object after adding attribute:" + jsonobject); Returns a Jsonarray object Jsonarray Jsonarray = new Jsonarray (); Jsonarray.add (0, "This is a Jsonarray value"); Jsonarray.add (1, "another Jsonarray value"); Jsonobject.element ("Jsonarray", Jsonarray); In the back of Jsonobject a jsonarray jsonarray array = Jsonobject.getjsonarray ("Jsonarray"); System.out.println (Jsonobject); System.out.println ("Returns a Jsonarray object:" + array); Add Jsonarray Value//{"username": "Huangwuyi", "Sex": "Male", "QQ": "413425430", "Min.score": ", nickname": "The Mood in the dream", "address ":" Xiamen, Fujian Province "," Jsonarray ": [" This is a Jsonarray value "," another Jsonarray value "]} System.out.println (" result = "+ Jsonobje CT); Returns a string based on key, string username = jsonobject.getstring ("username"); System.out.println ("username==>" + username); Converts a character to jsonobject String temp = jsonobJect.tostring (); Jsonobject object = jsonobject.fromobject (temp); Returns the value System.out.println ("qq=" + object.get ("QQ") after conversion according to Key; }}
Output Result:
jsonobject:{"username": "Huangwuyi", "Sex": "Male", "QQ": "413425430", "Min.score": "A", "nickname": "Dream Mood"} is an array: false, Null: False, Isnullobject:false the object after adding the attribute: {"username": "Huangwuyi", "Sex": "Male", "QQ": "413425430", "Min.score": 99, " Nickname ":" The Mood in the dream "," Address ":" Xiamen, Fujian Province "} {" username ":" Huangwuyi "," Sex ":" Male "," QQ ":" 413425430 "," Min.score ": 99," Nickname ":" The Mood in the dream "," Address ":" Xiamen, Fujian Province "," Jsonarray ": [" This is a Jsonarray value "," another Jsonarray value "]} Returns a Jsonarray object: ["This is a Jsonarray value", "another Jsonarray value"] Results ={"username": "Huangwuyi", "Sex": "Male", "QQ": " 413425430 "," Min.score ":" nickname ":" The Mood in the dream "," Address ":" Xiamen, Fujian Province "," Jsonarray ": [" This is a Jsonarray value "," another Jsonarray value "]}username==>huangwuyiqq=413425430
2.2. Example 2.
Package Jsontest;import Net.sf.json.jsonarray;import Net.sf.json.jsonobject;public class Jsontest {public static void Main (String args[]) {jsonobject jsonObj0 = new Jsonobject (); Jsonobject jsonobj = new Jsonobject (); Jsonobject jsonObj2 = new Jsonobject (); Jsonobject jsonObj3 = new Jsonobject (); Jsonarray Jsonarray = new Jsonarray (); Create JsonObj0 jsonobj0.put ("NAME0", "Zhangsan"); Jsonobj0.put ("Sex1", "female"); System.out.println ("jsonObj0:" +jsonobj0); Create JsonObj1 jsonobj.put ("name", "Xuwei"); Jsonobj.put ("Sex", "male"); System.out.println ("Jsonobj:" +jsonobj); Create JSONOBJ2, which contains two entries, the contents of which are jsonobj0,jsonobj1 jsonobj2.put ("Item0", jsonObj0), respectively; Jsonobj2.put ("Item1", jsonobj); System.out.println ("JsonObj2:" +jsonobj2); Create JSONOBJ3, with only one entry, content jsonObj2 jsonobj3.element ("J3", jsonObj2); System.out.println ("JsonObj3:" +jSONOBJ3); Add the Jsonobject object to the Jsonarray. found that the difference between Jsonarray and Jsonobject is jsonarray than jsonobject more brackets [] jsonarray.add (jsonobj); System.out.println ("Jsonarray:" +jsonarray); Jsonobject jsonObj4 = new Jsonobject (); Jsonobj4.element ("Weather", jsonarray); System.out.println ("JsonObj4:" +jsonobj4); }}
Output Result:
jsonobj0:{"NAME0": "Zhangsan", "sex1": "Female"}jsonobj:{"name": "Xuwei", "Sex": "Male"}jsonobj2:{"item0": {"NAME0": " Zhangsan "," sex1 ":" Female "}," Item1 ": {" name ":" Xuwei "," Sex ":" Male "}}jsonobj3:{" J3 ": {" item0 ": {" NAME0 ":" Zhangsan "," Sex1 ":" Female "}," Item1 ": {" name ":" Xuwei "," Sex ":" Male "}}}jsonarray:[{" name ":" Xuwei "," Sex ":" Male "}]jsonobj4:{" Weather ": [{" Name ":" Xuwei "," Sex ":" Male "}]}
Java generates JSON strings, Jsonobject, and Jsonarray use