繁体   English   中英

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

[英]Java code to traverse a json object and update Multiple values in a Json Object

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

我使用过API,但我不确定如何遍历多个数组中的json对象,并更新相同内容以获取更新详细信息的完整对象。

我必须更新attributes.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"
                        }
                    ]
                }

            }
            ]

    }
}

我尝试过的代码和dint工作:

@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以更新多个值并获取更新的对象。

尝试这个,

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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