Dependency Package: Json-20180130.jar
Maven Address:
1 <Dependency>2 <groupId>Org.json</groupId>3 <Artifactid>Json</Artifactid>4 <version>20180130</version>5 </Dependency>
Relatively simple, a look to understand, the source code is as follows:
1 PackageJSON;2 3 ImportOrg.json.JSONArray;4 ImportOrg.json.JSONObject;5 6 ImportJava.util.HashMap;7 ImportJava.util.Map;8 9 /**Ten * Jsonobject and Jsonarray use: For JSON object conversion and manipulation One * jsonobject: Processing JSON strings, a variety of construction methods, can be used for multi-type transfer. A * Jsonarray:json Array, sequential - */ - Public classJsonfirst { the Public Static voidMain (string[] args) { - //1. Define a JSON object -Jsonobject Jo =NewJsonobject (); -Jo.put ("id", "1"); +Jo.put ("name", "Tom"); -Jo.put ("Age", "25"); +System.out.println ("1, Jsonobject:" +Jo); A //Get Name Value atString name = jo.getstring ("name"); -SYSTEM.OUT.PRINTLN ("Query name:" +name); - - //2. Define a JSON array with the order of the arrays -Jsonarray ja =NewJsonarray (); -Ja.put (0, "Yang"); inJa.put (1, "Li"); -Ja.put (2, "Zhang"); toJa.put (4, "Fouth"); +System.out.println ("2, Jsonarray array:" +ja); - //get a fourth value theString Fouth = ja.getstring (4); *SYSTEM.OUT.PRINTLN ("Query fourth:" +fouth); $ Panax Notoginseng //3. Add JSON objects in JSON to make complex JSON -Jo.put ("Object", ja); theSystem.out.println ("3, after adding the JSON object:" +Jo); + A //4. String conversion to JSON (complex JSON processing) theString str = "{\" name\ ": \" tom\ ", \" object\ ": [\" yang\ ", \" Li\ ", \" zhang\ ", null,\" fouth\ "],\" id\ ": \" 1\ ", \" age\ ": \" 25\ " }"; +Jsonobject Jo2 =Newjsonobject (str); -System.out.println ("4, String converted to JSON:" +Jo2); $SYSTEM.OUT.PRINTLN ("Name attribute value:" + jo2.getstring ("name")); $System.out.println ("object property value:" + jo2.get ("Object")). toString ()); - //Delete object property value -Jo.remove ("Object"); theSystem.out.println ("Delete object property value:" +Jo); - Wuyi //5. Map and JSON conversion themap<string, string> map1 =NewHashmap<string, string>(); -Map1.put ("name", "Alexia"); WuMap1.put ("Sex", "female"); -Map1.put ("Age", "23"); AboutJsonobject Jo3 =NewJsonobject (MAP1); $SYSTEM.OUT.PRINTLN ("5.1 Map to JSON:" +Jo3); -map<string, object> map2 =NewHashmap<string, object>(); -MAP2 =Jo3.tomap (); -SYSTEM.OUT.PRINTLN ("5.2 JSON to map:" +map2); A + //6. Add map and JSON objects in Jsonarray theJsonarray Jo4 =NewJsonarray (); - Jo4.put (MAP1); $Jo4.put (1, "333"); the Jo4.put (Jo); theSystem.out.println ("6, Jsonarray Add map and JSON object:" +Jo4); the the } -}
Operation Result:
1. Jsonobject: {"name": "Tom", "id": "1", "Age": "25"}
Query Name:tom
2. Jsonarray array: ["Yang", "Li", "Zhang", null, "Fouth"]
Query Fourth: Fouth
3. After adding the JSON object: {"name": "Tom", "Object": ["Yang", "Li", "Zhang", null, "Fouth"], "id": "1", "Age": "25"}
4. String converted to json:{"name": "Tom", "Object": ["Yang", "Li", "Zhang", null, "Fouth"], "id": "1", "Age": "25"}
Name property value: Tom
object property Value: ["Yang", "Li", "Zhang", null, "Fouth"]
Delete the object property value: {"name": "Tom", "id": "1", "Age": "25"}
5.1 Map to json:{"name": "Alexia", "Age": "All", "sex": "Female"}
5.2 json to Map:{sex=female, Name=alexia, age=23}
6. Add map and JSON object in Jsonarray: [{"Name": "Alexia", "Age": "" "," "Sex": "Female"}, "333", {"name": "Tom", "id": "1", "Age": "25"} ]
JSON processing of Java (Jsonobject, Jsonarray)