Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
493 views
in Technique[技术] by (71.8m points)

merging two json objects into single object in java

I have two json objects like below:

{"name":["Karbonn Smart A12 Star (Black & Silver)","Nokia 220 (Black)","Karbonn Smart A52 Plus (Black & Gold)","Karbonn Smart A12 Star (White & Gold)",.......]}
{"price":["Rs. 3,699","Rs. 2,599","Rs. 2,499","Rs. 3,699",..........]}

I would like to combine both the objects like below I tried by using nested for each loop it did not worked I am not knowing the procedure to achieve this:

{"mobile":[{"name":"Karbonn Smart A12 Star (Black & Silver)","price":"Rs. 2,499"}]...........}

my code is below:

for(Element a:mobilename)
    {
    text= a.text();
    arr.add(text);
    obj1.put("name", arr);
    //a11.add(text);

}
   arr2.add(obj1);

    for(Element b:price)
    {
    text1=b.text();
    arr1.add(text1);

    obj.put("price", arr1);




     }
    arr2.add(obj1);
    arr2.add(obj);
    obj2.put("mobile", arr2);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can iterate via for loop and in every iteration create new JSONObject and add it to a collection. Finally add the collection to the mergedObject. E.g.

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collection;

public class Foo {
    public static void main(String[] args) throws JSONException {

        JSONObject object1 = new JSONObject("{
" +
                "    "name": [
" +
                "        "Karbonn Smart A12 Star (Black & Silver)",
" +
                "        "Nokia 220 (Black)",
" +
                "        "Karbonn Smart A52 Plus (Black & Gold)",
" +
                "        "Karbonn Smart A12 Star (White & Gold)",
" +
                "        "Karbonn Smart A50s (Black)",
" +
                "        "Karbonn Smart A52 Plus (White & Silver)",
" +
                "        "Karbonn Titanium S2 Plus (White)",
" +
                "        "Karbonn Smart A11 Star (Black)",
" +
                "        "Karbonn Smart A11 Star (White)"
" +
                "    ]
" +
                "}");

        JSONObject object2 = new JSONObject("{
" +
                "    "price": [
" +
                "        "Rs. 3,699",
" +
                "        "Rs. 2,599",
" +
                "        "Rs. 2,499",
" +
                "        "Rs. 3,699",
" +
                "        "Rs. 2,699",
" +
                "        "Rs. 2,499",
" +
                "        "Rs. 4,999",
" +
                "        "Rs. 4,399",
" +
                "        "Rs. 4,499"
" +
                "    ]
" +
                "}");


        JSONArray nameArray = object1.getJSONArray("name");
        JSONArray priceArray = object2.getJSONArray("price");

        JSONObject mergedObject = new JSONObject("{}");
        Collection<JSONObject> collection = new ArrayList<>();

        for (int i = 0; i < nameArray.length(); i++) {
            JSONObject obj = new JSONObject();
            obj.put("name", nameArray.getString(i));
            obj.put("price", priceArray.getString(i));
            collection.add(obj);
        }

        mergedObject.put("mobile", collection);
        System.out.println(mergedObject);
    }
}

Ouput:

{"mobile":[{"price":"Rs. 3,699","name":"Karbonn Smart A12 Star (Black & Silver)"},{"price":"Rs. 2,599","name":"Nokia 220 (Black)"},{"price":"Rs. 2,499","name":"Karbonn Smart A52 Plus (Black & Gold)"},{"price":"Rs. 3,699","name":"Karbonn Smart A12 Star (White & Gold)"},{"price":"Rs. 2,699","name":"Karbonn Smart A50s (Black)"},{"price":"Rs. 2,499","name":"Karbonn Smart A52 Plus (White & Silver)"},{"price":"Rs. 4,999","name":"Karbonn Titanium S2 Plus (White)"},{"price":"Rs. 4,399","name":"Karbonn Smart A11 Star (Black)"},{"price":"Rs. 4,499","name":"Karbonn Smart A11 Star (White)"}]}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...