Java代码遍历json对象并更新Json对象中的多个值

时间:2019-04-08 06:20:38

标签: java json

我的要求是使用Java unirestapi进行get请求并获取jsonObject响应,更新该对象中的多个字段值,并执行unirest put请求以将更新后的json对象发回以更新我的应用程序中的记录。

我曾经使用过API,但是我不确定如何遍历多个数组中的json对象并更新该对象以获取具有更新详细信息的完整对象。

我必须更新属性.Country.value,attributes.Name.value,attributes.Address.value.AddressLine1.value并放回更新后的主体以更新UI中的数据。

要更新的Json对象:

{
    "uri": "entities/18YU6afW",
    "type": "configuration/entityTypes/HCO",
    "createdBy": "vkrishnan",
    "createdTime": 1554648322649,
    "updatedBy": "vkrishnan",
    "updatedTime": 1554648322649,
    "attributes": {
        "Country": [
            {
                "type": "configuration/entityTypes/HCO/attributes/Country",
                "ov": true,
                "value": "US-US",
                "lookupCode": "US",
                "lookupRawValue": "US",
                "uri": "entities/18YU6afW/attributes/Country/26p9f0lJ8"
            }
        ],
        "Name": [
            {
                "type": "configuration/entityTypes/HCO/attributes/Name",
                "ov": true,
                "value": "Fortis Hospital",
                "uri": "entities/18YU6afW/attributes/Name/26p9f0h2s"
            }
        ],
        "Address": [
            {
                "label": "PO BOX 661 BUNDABERG QLD 4670",
                "relationshipLabel": "",
                "value": {
                    "AddressLine1": [
                        {
                            "type": "configuration/entityTypes/Location/attributes/AddressLine1",
                            "ov": true,
                            "value": "PO BOX 661",
                            "uri": "entities/18YU6afW/attributes/Address/f2nDWl4/AddressLine1/5hIui3tr"
                        }
                    ]
                }

            }
            ]

    }
}

我尝试过并努力工作的代码:

@Step
    public static JSONObject updateJsonResponse(JSONObject mdmresponse, String mapperField,String newvalue) {
        System.out.println("****responseJSON****"+mdmresponse);
        System.out.println("****mapperField****"+mapperField);
        try {
            JsonObject Response = null;
            JsonObject Response2 = null;
            JsonParser parser = new JsonParser();
            if (mdmresponse.has(mapperField)) {
                //responseJSON.put(resp, value)
            //  return String.valueOf(responseJSON.get(mapperField));
            } else {
                String[] Mapper = mapperField.split("\\.");
                Response = parser.parse(mdmresponse.toString()).getAsJsonObject();
                for (int i = 0; i < Mapper.length; i++) {

                    JsonElement element = parser.parse(String.valueOf(Response.get(Mapper[i])));
                    if (element.isJsonArray()) {
                        element = element.getAsJsonArray().get(0).getAsJsonObject();
                    }

                    try{
                        Response = element.getAsJsonObject();
                    if (i == Mapper.length - 2) {
                        Set<Map.Entry<String, JsonElement>> entries = Response.entrySet();
                        for (Map.Entry<String, JsonElement> entry : entries) {
                            if (entry.getKey().equals(Mapper[Mapper.length - 1])) {

                                System.out.println("******entry.getKey()*****"+entry.getKey());
                                Response.addProperty(entry.getKey(),newvalue);
                                JsonObject gson = new JsonParser().parse(Response.toString()).getAsJsonObject();

                                JSONObject jo2 = new JSONObject(gson.toString());
                                //mdmresponse.put(key, value)
                                //responseJSON.put(entry.getKey(),newvalue);
                                //responseJSON.put(entry.getKey(),newvalue);
                                //return String.valueOf(Response.get(entry.getKey()));
                                //return Response;



                                return jo2;

                            }
                        }
                        break;
                    }
                    }
                    catch(Exception e)
                    {

                        System.out.println("Field not found in response");
                        e.printStackTrace();
                    }

                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

任何人都可以通过Java中的适当方法来帮助我遍历json以更新多个值并取回该更新的对象。

1 个答案:

答案 0 :(得分:0)

尝试一下

JSONObject attributes= mdmresponse.getJSONObject("attributes");

Set<?> attributesKeySet=  attributes.keySet();

    Iterator<?> attributesKeySetItr = attributesKeySet.iterator();
    do{
        String key = attributesKeySetItr.next().toString();

        JSONArray attributeArray= attributes.getJSONArray(key);

            for (int i = 0; i < attributeArray.length(); i++) {
                String value= attributeArray.getJSONObject(i).getString("value");
                System.out.println(value);

              // Process the value of attribute here


            }


    }while(attributesKeySetItr.hasNext());