How to use JSONOBJECT and JSONARRAY in Java

tags: java

public static void main(String[] args) {

        // Create a JSONOBJECT object
        JSONObject jsonObject1 = new JSONObject();

        // MAP and JSONOBJECT mutual conversion
        Map<String, String> map1 = new HashMap<String, String>();
        map1.put("we", "dsds");
        JSONObject jsonObject2 = JSONObject.fromObject(map1);
        Map<String, String> map2 = (Map)JSONObject.toBean(jsonObject2, Map.class);

       // string to turn JSONObject
        JSONObject jsonObject3 = JSONObject.fromObject("{obj: [{name: 'wwp'}, {age: 22}], obj: 2}");

        // put () and get () method
        jsonObject2.put("key", "sds");
        // Note: If the key does not exist, throw an exception
        jsonObject2.get("key");  
public class JSONtest {  


    public static void main(String args[]){  

        // Create a JSONOBJECT object
        JSONObject jsonObject = new JSONObject();  
        jsonObject.put("key1", "value1");  
        jsonObject.put("key2", "value2");  
        jsonObject.put("key3", "value3");  
        jsonObject.put("key4", "value4");  
        System.out.println("jsonObject:"+jsonObject);  

                 // Create a JSonArray object  
        JSONArray jsonArray=new JSONArray();  
        jsonArray.add("value1");  
        jsonArray.add("value2");  
        jsonArray.add("value3");  
        jsonArray.add("value4");  
        System.out.println("jsonArray:"+jsonArray);  


                 // map to jsonobject  
        Map<String, String> map=new HashMap<String, String>();  
        map.put("key1", "value1");  
        map.put("key2", "value2");  
        map.put("key3", "value3");  
        map.put("key4", "value4");  
        System.out.println("jsonObject2:"+JSONObject.fromObject(map));        

                 // list into jsonarray  
        List<String> list=new ArrayList<String>();  
        list.add("value1");  
        list.add("value2");  
        list.add("value3");  
        System.out.println("jsonArray2:"+JSONArray.fromObject(list));  

                 // JsonArray parsing MAP will put the entire JSON object in an array.  
        System.out.println("jsonArray from map:"+JSONArray.fromObject(map));  

                 // put JSONOBJECT in JSonarray  
        JSONObject jsonObject3=new JSONObject();  
        jsonObject3.put("Name", "Zhang San");  
        jsonObject3.put("age", "13");  
        jsonObject3.put("height", "166");  
        System.out.println("Put JSONOBJECT in JSonarray:"+JSONArray.fromObject(jsonObject3));  

    }  

}  

Print output

jsonObject:{"key4":"value4","key3":"value3","key2":"value2","key1":"value1"}
jsonArray:["value1","value2","value3","value4"]
jsonObject2:{"key4":"value4","key3":"value3","key2":"value2","key1":"value1"}
jsonArray2:["value1","value2","value3"]
jsonArray from map:[{"key4":"value4","key3":"value3","key2":"value2","key1":"value1"}]
 Put JSONOBJECT in JSonarray: [{"Name":"Zhang San","height":"166","age":"13"}]

Differences between PUT, Accumulate, Element
public object put (Object Key, Object value) maps the value to Key. If this JSONOBJECT object exists, there is a value under this key, the current value will replace the previous Value.

[java] view plain copy
JSONObject jsonObject = new JSONObject();  
jsonObject.put("key1", "value1");  
jsonObject.put("key2", "value2");  
jsonObject.put("key2", "value2");  
jsonObject.put("key4", "value4");  
System.out.println("jsonObject:"+jsonObject);  

Output:

jsonObject:{"key4":"value4","key2":"value2","key1":"value1"}

It was found that Key2 was covered.

Public JsonObject Accumulate (String Key, Object Value) Accumulate Value to this key. This method is similar to the element () method, special, if there is already a value currently in this key, then a JSonarray will be stored in this key to save all accumulated Value. If there is already a JSonarray, the current value will be added to this JsonArray. In contrast, the Replace method replaces the previous Value, the code is as follows.

[java] view plain copy
JSONObject jsonObject = new JSONObject();  
jsonObject.accumulate("key1", "value1");  
jsonObject.accumulate("key2", "value2");  
jsonObject.accumulate("key2", "value2");  
jsonObject.accumulate("key4", "value4");  
System.out.println("jsonObject:"+jsonObject);  

Output:

jsonObject:{"key4":"value4","key2":["value2","value2"],"key1":"value1"} 

Value accumulates to Key2, the value of Key2 is an array.

Public JSONObject Element (String Key, Object Value) puts the key / value in this JsonObject object. So if this key exists, the current value is empty, this key will be removed. If this Key has a value value, then this method calls the Accumulate () method. This is the statement of official documentation, but through the experiment finds that there is no invocumulate () method if Key repeats, then override. code show as below:

[java] view plain copy
JSONObject jsonObject = new JSONObject();  
jsonObject.element("key1", "value1");  
jsonObject.element("key2", "value2");  
jsonObject.element("key2", "value2");  
jsonObject.element("key2", "value4");  
System.out.println("jsonObject:"+jsonObject);  

Output:

jsonObject:{"key2":"value4","key1":"value1"}

KEY2 is covered and is not accumulated.

Intelligent Recommendation

The use of JSONArray and JSONObject

Mainly use Array to return the specified data format. The code sample is as follows:  ...

JSONOBJECT is simple to use with Jsonarray

JSONObject Everyone sees JSONOBJECT should really be related to JSON format, yes, JsonObject is a class that will be data JSON, which is a key-value form and MAP type compare. Let's take a look at thi...

Use of JsonArray and JsonObject

Transfer from https://my.oschina.net/zimingforever/blog/62066 Below is a very imageive example found from the Internet, running locally. import net.sf.json.JSONArray; import net.sf.json.JSONObject; pu...

JsonObject and Jsonarray simple use

JsonObject and Jsonarray simple use Foreword It has always been used to install or respond data with an object. This is also a bad place. Benefits: The specific data of the request or response corresp...

JAVA is used JSONArray and JSONObject

json Is a key value corresponds to a simple one to one relationship. JSONObject json objectsIs a value corresponding to a key (Key-value pairs), Using braces {} such as: {key: value} JSONArray json ar...

More Recommendation

Traversal in Java, JsonObject and Jsonarray

JSONOBJCET traverses: Jsonarray traverses:...

Java analysis JSONObject / JSonArray

JAVA analysis jasonObject Java analysis jsonarray...

JAVA Processing JSONOBJECT JSONARRAY

In the process of learning Java, we will inevitably encounter to process JSON data formats, and now you will introduce JSON in Java. 1, handle JSONOBJECT 2, handle JSonarray Simple and easy to underst...

Java-jsonObject to jsonarray, jsonarray to list object

JsonObject to JSONARRAY JSONArray data = JSONArray.parseArray(reqMap.get("userListDto").toString()); JSONObject jsonObj = JSONObject.fromObject(resultMap.get("data")); JSONArray ce...

Java study notes 50: JSONObject and JSONArray use (turn)

Unlike Java and PHP parsing JSON always produce a more painful process. But using JSONObject and JSONArray will make the whole process is relatively comfortable. The need to rely on packet: commons-la...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top