在java中将两个json对象合并为单个对象

时间:2014-12-13 10:25:38

标签: java json

我有两个json对象,如下所示:

{"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",..........]}

我想结合下面的对象,我尝试使用嵌套为每个循环它没有工作我不知道实现这个的过程:

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

我的代码如下:

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);

3 个答案:

答案 0 :(得分:3)

您可以通过for循环迭代,并在每次迭代中创建新的JSONObject并将其添加到集合中。最后将该集合添加到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("{\n" +
                "    \"name\": [\n" +
                "        \"Karbonn Smart A12 Star (Black & Silver)\",\n" +
                "        \"Nokia 220 (Black)\",\n" +
                "        \"Karbonn Smart A52 Plus (Black & Gold)\",\n" +
                "        \"Karbonn Smart A12 Star (White & Gold)\",\n" +
                "        \"Karbonn Smart A50s (Black)\",\n" +
                "        \"Karbonn Smart A52 Plus (White & Silver)\",\n" +
                "        \"Karbonn Titanium S2 Plus (White)\",\n" +
                "        \"Karbonn Smart A11 Star (Black)\",\n" +
                "        \"Karbonn Smart A11 Star (White)\"\n" +
                "    ]\n" +
                "}");

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


        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);
    }
}

输出继电器:

{"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)"}]}

答案 1 :(得分:2)

你可以在这里找到org.json JSON-java

import java.util.ArrayList;
import java.util.List;
import org.json.*;

public class JsonTest
{   
    public String mergeJson(String name, String price)
    {
        JSONObject nameJ= new JSONObject(name);
        JSONObject priceJ= new JSONObject(price);
        JSONObject mobileJ = new JSONObject();

        JSONArray names = nameJ.getJSONArray("name");
        JSONArray prices = priceJ.getJSONArray("price");
        JSONArray mobiles = new JSONArray();

        if(names.length() == prices.length())
        {
            for(int i=0;i<names.length();i++)
            {
                JSONObject mobile = new JSONObject();
                mobile.put("name", names.getString(i));
                mobile.put("price", prices.getString(i));
                mobiles.put(mobile);
            }
        }

        mobileJ.put("mobile", mobiles);

        return mobileJ.toString();          
    }

    public static void main(String[] args)
    {
        String name = "{'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)']}";
        String price = "{'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']}";

        JsonTest test = new JsonTest();
        System.out.println(test.mergeJson(name,price));
    }

}

答案 2 :(得分:0)

合并JsonObject(gson)-

    JsonObject data = new JsonObject();
    data = receivedJsoData.get("details").getAsJsonObject();

    JsonObject data2 = new JsonObject();
    data2 = receivedJsoData1.get("details").getAsJsonObject();

    JsonObject mergedData = new JsonObject();

    Set<Map.Entry<String, JsonElement>> entries = data1.entrySet();  //will return members of your object
    for (Map.Entry<String, JsonElement> entry : entries) {
        mergedData.add(entry.getKey(), entry.getValue());
    }
    Set<Map.Entry<String, JsonElement>> entries1 = data2.entrySet();  //will return members of your object
    for (Map.Entry<String, JsonElement> entry : entries1) {
        mergedData.add(entry.getKey(), entry.getValue());
    }