merging two json objects into single object in java

后端 未结 3 1928
有刺的猬
有刺的猬 2020-12-18 08:03

I have two json objects like below:

{\"name\":[\"Karbonn Smart A12 Star (Black & Silver)\",\"Nokia 220 (Black)\",\"Karbonn Smart A52 Plus (Black & Go         


        
相关标签:
3条回答
  • 2020-12-18 08:40

    Merge 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());
        }
    
    0 讨论(0)
  • 2020-12-18 08:41

    you can find org.json here 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));
        }
    
    }
    
    0 讨论(0)
  • 2020-12-18 08:42

    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("{\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);
        }
    }
    

    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)"}]}
    
    0 讨论(0)
提交回复
热议问题