1. Introduction to JAR package
To make the program run, the JSON-lib package must be introduced, which also depends on the following JAR packages:
1.commons-lang.jar
2.commons-beanutils.jar
3.commons-collections.jar
4.commons-logging.jar
5.ezmorph.jar
6.json-lib-2.2.2-jdk15.jar
2.JSONObject object usage
The JSON-lib package is a package that converts beans, collections, maps, java arrays and XML and JSON. In this case, we will use the JSONObject class to create JSONObject objects, and then we print the values of those objects. In order to use the JSONObject object, we want to introduce the "net.sf.json" package. To add elements to the object, we need to use the put() method.
2.1. Example 1
package jsontest;import net.sf.json.JSONArray;import net.sf.json.JSONObject;public class JSONObjectSample { // Create JSONObject 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", "Dream Center"); return jsonObject; } public static void main(String[] args) { JSONObject jsonObject = JSONObjectSample.createJSONObject();// Wait for the method, call directly through the class name + method // Output jsonobject object System.out.println("jsonObject:" + jsonObject); // interpret the type of output object boolean isArray = jsonObject.isArray(); boolean isEmpty = jsonObject.isEmpty(); boolean isNullObject = jsonObject.isNullObject(); System.out.println("Is it an array:" + isArray + ", isNullObject:" + isNullObject); // Add attributes and append elements after jsonObject. jsonObject.element("address", "Xiamen City, Fujian Province"); System.out.println("A object after adding attributes: " + jsonObject); // Return 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); //Reside a jsonArray behind jsonObject JSONArray array = jsonObject.getJSONArray("jsonArray"); System.out.println(jsonObject); System.out.println("Return a JSONArray object: " + array); // Add the value after JSONArray// {"username":"huangwuyi","sex":"Male","QQ":"413425430","Min.score":99,"nickname":"Dream center","address":"Xiamen City, Fujian Province","jsonArray":["this is a jsonArray value","another jsonArray value"]} System.out.println("result=" + jsonObject); // Return a string according to the key String username = jsonObject.getString("username"); System.out.println("username==>" + username); // Convert characters to JSONObject String temp = jsonObject.toString(); JSONObject object = JSONObject.fromObject(temp); // After conversion, return value based on Key System.out.println("qq=" + object.get("QQ")); }} Output result
jsonObject: {"username":"huangwuyi","sex":"male","QQ":"413425430","Min.score":99,"nickname":"Dream Center"}
Whether it is an array: false,
Whether it is empty: false,
isNullObject:false object after adding attribute:
{"username":"huangwuyi",
"sex":"male",
"QQ":"413425430",
"Min.score":99,"nickname":"Dream Center",
"address":"Xiamen City, Fujian Province"}
{"username":"huangwuyi","sex":"male","QQ":"413425430","Min.score":99,"nickname":"Dream center",
"address":"Xiamen City, Fujian Province","jsonArray":["this is a jsonArray value","another jsonArray value"]}
Return a JSONArray object: ["this is a jsonArray value","another jsonArray value"]
Results = {"username":"huangwuyi","sex":"male",
"QQ":"413425430","Min.score":99,"nickname":"Dream center","address":"Xiamen City, 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, contains two entries, the contents of the entries are jsonObj0, jsonObj1 jsonObj2.put("item0", jsonObj0); jsonObj2.put("item1", jsonObj); System.out.println("jsonObj2:"+jsonObj2); //Create jsonObj3, there is only one entry, the contents are jsonObj2 jsonObj3.element("j3", jsonObj2); System.out.println("jsonObj3:"+jsonObj3); //Add JSONObject object to JSONArray. Found that the difference between JSONArray and JSONObject is that JSONArray has more brackets than JSONObject[] 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"}]}
The above is the knowledge of using JSONObject and JSONArray introduced to you by the editor. I hope it will be helpful to you. If you want to know more, please pay attention to the Wulin.com website!