简体   繁体   中英

Add JSON object into existing JSON array using java

Here this is my JSON which I have as a string "additionalThirdParty"

{
    "header": null,
    "object": {
        "ASSETS": {
            "productDetails": [{
                "productId": "PT_230",
                "productThirdPartyDetails": [{
                    "thirdPartyId": "TH12",
                    "Location": "France",
                    "addtionalInfo": []
                }]
            }]
        }
    }
}

Now my requirement is to add an extra JSON object inside "productThirdPartyDetails" array.

The new JSON object will be

{
    "thirdPartyId": "TH11",
    "Location": "Belgium",
    "addtionalInfo": []
}

So the final object should be like

{
    "header": null,
    "object": {
        "ASSETS": {
            "productDetails": [{
                "productId": "PT_230",
                "productThirdPartyDetails": [{
                        "thirdPartyId": "TH12",
                        "Location": "France",
                        "addtionalInfo": []
                    },
                    {
                        "thirdPartyId": "TH11",
                        "Location": "Belgium",
                        "addtionalInfo": []
                    }
                ]
            }]
        }
    }
}

I'm trying to add as follows.

converting this string to JSON as

    JSONObject obj = new JSONObject(additionalThirdParty);

    Iterator<?> keys = obj.keys();

    while (keys.hasNext()) {
        JSONObject assetsObj = obj.getJSONObject("ASSETS");
        Iterator<?> assetKeys = assetsObj.keys();
        while (assetKeys.hasNext()) {
            JSONArray productDetails = assetsObj.getJSONArray("productDetails");
            logger.info("productDetails=" + productDetails);
        }
    }

It throws me that org.json.JSONException: JSONObject["ASSETS"] not found.

Any ideas on how to add a new object into that array

In order to achieve what you want, you have to properly reach out to productThirdPartyDetails as shown below, then you have to identify the way of getting JSONObject that needs to be added, I have hardcoded that part it's better to get that object through a method.

JSONObject obj = new JSONObject(additionalThirdParty);


        JSONObject objtobeadded =  new JSONObject();
        objtobeadded.put("thirdPartyId", "TH11");
        objtobeadded.put("Location", "Belgium");
        objtobeadded.put("addtionalInfo", new JSONArray());

        JSONObject assetsObj = obj.getJSONObject("object").getJSONObject("ASSETS");

        JSONArray prodDetailsArr = assetsObj.getJSONArray("productDetails");

        for(int i=0;i<prodDetailsArr.length();i++){            
            JSONArray arr = prodDetailsArr.getJSONObject(i).getJSONArray("productThirdPartyDetails");
            arr.put(objtobeadded);
        }
        System.out.println(obj.toString());
JSONObject assetsObj = obj.getJSONObject("ASSETS"); here the ASSETS is in under object,

So you should write obj.getJSONObject("object").getJSONObject("ASSETS"); to get the ASSETS object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM