Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to combine (merge) 2 JSONObjects?

Tags:

java

json

android

What is the best way to combine (merge) two JSONObjects?

JSONObject o1 = {
    "one": "1",
    "two": "2",
    "three": "3"
}
JSONObject o2 = {
        "four": "4",
        "five": "5",
        "six": "6"
    }

And result of combining o1 and o2 must be

JSONObject result = {
        "one": "1",
        "two": "2",
        "three": "3",
        "four": "4",
        "five": "5",
        "six": "6"
    }
like image 890
jimpanzer Avatar asked Oct 24 '13 12:10

jimpanzer


People also ask

How do I combine two JSON objects?

JSONObject to merge two JSON objects in Java. We can merge two JSON objects using the putAll() method (inherited from interface java.

How do I combine two JSON responses?

Solution 2 - Use concat() for arrays Assuming that you would like to merge two JSON arrays like below: var json1 = [{id:1, name: 'xxx' ...}] var json2 = [{id:2, name: 'xyz' ...}]

How do I combine two Jsonarrays?

simple. JSONArray class to merge two JSON arrays in Java. We can merge two JSON arrays using the addAll() method (inherited from interface java.

How do I combine two JSON objects with the same key?

Use concat() for arrays Assuming that you would like to merge two JSON arrays like below: var json1 = [{id:1, name: 'xxx' ...}] var json2 = [{id:2, name: 'xyz' ...}]


2 Answers

I have your same problem: I can't find the putAll method (and it isn't listed in the official reference page).

So, I don't know if this is the best solution, but surely it works quite well:

//I assume that your two JSONObjects are o1 and o2
JSONObject mergedObj = new JSONObject();

Iterator i1 = o1.keys();
Iterator i2 = o2.keys();
String tmp_key;
while(i1.hasNext()) {
    tmp_key = (String) i1.next();
    mergedObj.put(tmp_key, o1.get(tmp_key));
}
while(i2.hasNext()) {
    tmp_key = (String) i2.next();
    mergedObj.put(tmp_key, o2.get(tmp_key));
}

Now, the merged JSONObject is stored in mergedObj

like image 170
Vito Gentile Avatar answered Oct 13 '22 04:10

Vito Gentile


json objects to be merge in that new json object like this.

    JSONObject jObj = new JSONObject();
    jObj.put("one", "1");
    jObj.put("two", "2");
    JSONObject jObj2 = new JSONObject();
    jObj2.put("three", "3");
    jObj2.put("four", "4");


    JSONParser p = new JSONParser();
    net.minidev.json.JSONObject o1 = (net.minidev.json.JSONObject) p
                        .parse(jObj.toString());
    net.minidev.json.JSONObject o2 = (net.minidev.json.JSONObject) p
                        .parse(jObj2.toString());

    o1.merge(o2);

    Log.print(o1.toJSONString());

now o1 will be the merged json object. you will get the output like this ::

{"three":"3","two":"2","four":"4","one":"1"}

please refer this link and download the smartjson library ..here is the link http://code.google.com/p/json-smart/wiki/MergeSample

hope it will help.

like image 41
Nirav Tukadiya Avatar answered Oct 13 '22 04:10

Nirav Tukadiya