JSONObject, JSONArray of fastjson

JSONObject, JSONArray are two subclasses of JSON.

 

First let's look at the JSONObject source code:

You will find that JSONObject inherits Map<String, Object>, and uses the methods in Map. It can be said that JSONObject is equivalent to Map<String, Object>

Take a look at a specific column:

  /**
     * Convert Map to JSONObject, then add elements, output
     */
    @Test
    public  void testJsonObject() {
        Map<String, Object> testMap = new HashMap<>();
        testMap.put("key1", "value1");
        testMap.put("key2", "value2");
        
        JSONObject jsonObj = new JSONObject(testMap);
        jsonObj.put("key3", "value3");
        System.out.println(jsonObj);
        System.out.println(jsonObj.get("key2"));
    }

operation result:

{"key1":"value1","key2":"value2","key3":"value3"}
value2

 

Look at the source code of JSONArray:

 

 You will find that JSONArray inherits List<Object> and uses the methods in List. It can be said that JSONArray is equivalent to List<Object>

Specific columns:

  /**
     * Convert the List object to JSONArray, and then output
     */
    @Test
    public  void testJsonArray() {
        List<Object> list = new ArrayList<>();
        list.add("home");
        list.add(60);
        list.add(true);
        list.add(new XwjUser(1, "Hello World", new Date()));
        
        JSONArray jsonArr = JSONArray.parseArray(JSON.toJSONString(list));
        System.out.println(jsonArr);
    }

operation result:

["home",60,true,{"id":1,"message":"Hello World","sendTime":1525237337937}]

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325256858&siteId=291194637