Use of JSONObject and JSONArray

1. Introduction to JAR package

To make the program run, the JSON-lib package must be imported. The JSON-lib package also depends 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 usage

The JSON-lib package is a package that converts beans, collections, maps, java arrays and XML to JSON. In this example, we will create JSONObject objects using the JSONObject class, and then we will print the values ​​of these objects. In order to use JSONObject objects, we need to import 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 JSONObject object
    private static JSONObject createJSONObject() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("username", "huangwuyi");
        jsonObject.put("sex", "男");
        jsonObject.put("QQ", "413425430");
        jsonObject.put("Min.score", new Integer(99));
        jsonObject.put("nickname", "梦中心境");
        return jsonObject;
    }

    public static void main(String[] args) {
        JSONObject jsonObject = JSONObjectSample.createJSONObject();//Waiting for the method, call directly through the class name + method
        // output jsonobject object
        System.out.println("jsonObject:" + jsonObject);

        // Read the type of the output object
        boolean isArray = jsonObject.isArray();
        boolean isEmpty = jsonObject.isEmpty();
        boolean isNullObject = jsonObject.isNullObject();
        System.out.println("Is it an array: " + isArray + ", is it empty: " + isEmpty
                + ", isNullObject:" + isNullObject);

        // Add attributes, append elements after jsonObject.
        jsonObject.element("address", "Xiamen City, Fujian Province");
        System.out.println("Object after adding properties: " + 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);
        //Live 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":"男","QQ":"413425430","Min.score":99,"nickname":"梦中心境","address":"福建省厦门市","jsonArray":["this is a jsonArray value","another jsonArray value"]}
        System.out.println("result=" + jsonObject);

        // return a string based on key
        String username = jsonObject.getString("username");
        System.out.println("username==>" + username);

        // Convert character to JSONObject
        String temp = jsonObject.toString();
        JSONObject object = JSONObject.fromObject(temp);
        // Return value according to Key after conversion
        System.out.println("qq=" + object.get("QQ"));

    }

}
output result
jsonObject:{"username":"huangwuyi","sex":"男","QQ":"413425430","Min.score":99,"nickname":"梦中心境"}
Is it an array: false, is it empty: false, isNullObject: false
Object after adding attributes: {"username":"huangwuyi","sex":"Male","QQ":"413425430","Min.score":99,"nickname":"Dreamland"," address":"Xiamen City, Fujian Province"}
{"username":"huangwuyi","sex":"男","QQ":"413425430","Min.score":99,"nickname":"梦中心境","address":"福建省厦门市","jsonArray":["this is a jsonArray value","another jsonArray value"]}
Returns a JSONArray object: ["this is a jsonArray value","another jsonArray value"]
结果={"username":"huangwuyi","sex":"男","QQ":"413425430","Min.score":99,"nickname":"梦中心境","address":"福建省厦门市","jsonArray":["this is a jsonArray value","another jsonArray value"]}
username==>huangwuyi
qq=413425430
2.2. Example 2.
ackage 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 content of which is jsonObj0, jsonObj1
        jsonObj2.put("item0", jsonObj0);
        jsonObj2.put("item1", jsonObj);
        System.out.println("jsonObj2:"+jsonObj2);
        
        //Create jsonObj3, only one entry, the content is jsonObj2
        jsonObj3.element("j3", jsonObj2);
        System.out.println("jsonObj3:"+jsonObj3);
    
        //Add JSONObject object to JSONArray. It is 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"}]}

Reprinted from: http://www.cnblogs.com/xwdreamer/archive/2011/12/16/2296904.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326926652&siteId=291194637