如何在不遍历整个json的情况下检索和更新json数组元素 [英] How to retrieve and update json array element without traversing entire json

查看:183
本文介绍了如何在不遍历整个json的情况下检索和更新json数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常复杂的json结构.它包含许多array elements,并且那些数组元素包含其他数组元素,依此类推. 请参见下面的json树结构.

I have a very complex json structure. It contains many array elements and those array elements contains other array elements and so on.. Please see below json tree structure.

Json树结构1:

Json树结构2:

正如上面以黄色突出显示的那样,我想更新"rdKey"字段的值. 我写了下面的代码,它工作正常:

As highlighted above in yellow, I want to update the value of "rdKey" field. I wrote below code and it is perfectly working fine :

String json = "escaped string (as it's a big string, I can't put it here)";
JSONObject jsonObj = new JSONObject(json);

    if (jsonObj.has("responseMap")) {
        JSONObject responseMap = jsonObj.getJSONObject("responseMap");
        if (responseMap.has("ValueJson")) {
            JSONObject valueJson = responseMap.getJSONObject("ValueJson");
            if (valueJson.has("ticketBean_CM")) {
                JSONObject ticketBean_CM = valueJson.getJSONObject("ticketBean_CM");
                if (ticketBean_CM.has("addByGamma")) {
                    String addByGamma = ticketBean_CM.getString("addByGamma");
                    System.out.println(addByGamma);

                    if (addByGamma.equals("VCE")) {
                        if (responseMap.has("ScreenJson")) {
                            JSONObject screenJson = responseMap.getJSONObject("ScreenJson");
                            if (screenJson.has("sections")) {
                                JSONArray sectionArray1 = screenJson.getJSONArray("sections");
                                if (sectionArray1.length() > 0) {
                                    JSONObject section0 = sectionArray1.getJSONObject(0);
                                    if (section0.has("sections")) {
                                        JSONArray sectionArray2 = section0.getJSONArray("sections");
                                        if (sectionArray2.length() > 3) {
                                            JSONObject section6 = sectionArray2.getJSONObject(3);
                                            if (section6.has("sections")) {
                                                JSONArray sectionArray3 = section6.getJSONArray("sections");
                                                if (sectionArray3.length() > 1) {
                                                    JSONObject section8 = sectionArray3.getJSONObject(1);
                                                    if (section8.has("elements")) {
                                                        JSONArray elementsArray1 = section8
                                                                .getJSONArray("elements");
                                                        if (elementsArray1.length() > 0) {
                                                            JSONObject elements1 = elementsArray1.getJSONObject(0);
                                                            if (elements1.has("elements")) {
                                                                JSONArray elementsArray2 = elements1
                                                                        .getJSONArray("elements");
                                                                if (elementsArray2.length() > 4) {
                                                                    JSONObject elements2 = elementsArray2
                                                                            .getJSONObject(4);
                                                                    if (elements2.has("rdKey")) {
                                                                        System.out.println(
                                                                                elements2.getString("rdKey"));
                                                                        elements2.put("rdKey",
                                                                                "CircuitID(FullPartial)");
                                                                        System.out.println(
                                                                                elements2.getString("rdKey"));
                                                                        System.out.println(jsonObj.toString());
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

如果有更好的解决方案,我希望你们能帮助我.我可以在不遍历整个json对象的情况下做到这一点(直到找到相关字段)吗?如果json树结构发生更改,则此解决方案将不起作用,作为此代码的成功方案,它必须是静态的.

I want you guys to help me if there is any better solution for this. Can I do it without traversing the entire json object (till I find the concerned field) ? This solution will not work if json tree structure gets changes, it needs to be static as a success scenario of this code.

请提出更好的解决方案.

Please suggest better solution.

推荐答案

如果要转义JSON遍历,则可以使用JSONPointer,该库在同一org.json库中可用. 例如:

If you want to escape traversing of JSON then you can use JSONPointer, available in same org.json library. E.g.:

String query = <json_pointer_query to element array>
JSONPointer pointer = new JSONPointer(query);
JSONObject elementsArrayJSON = (JSONObject) pointer.queryFrom(jsonObj);
elementsArrayJSON.put("rdKey","CircuitID(FullPartial)");

JSON指针查询语言可以通过以下方式引用:

JSON Pointer query language can be referred in:

https://tools.ietf.org/html/rfc6901

注意: JSON指针非常基础,它不支持通配符.因此,您需要确定元素名称,否则会引发异常.

Note: JSON Pointer is pretty basic, it doesn't support wild card. So you need to be sure about element names, otherwise it would throw exception.

这篇关于如何在不遍历整个json的情况下检索和更新json数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆