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结构。它包含许多数组元素,那些数组元素包含其他数组元素等等。请参阅下面的json树结构。
Json Tree Structure-1 :
Json树结构-1:
Json Tree Structure-2 :
Json树结构-2:
As highlighted above in yellow, I want to update the value of "rdKey
" field. I wrote below code and it is perfectly working fine :
如上面以黄色突出显示,我想更新“rdKey”字段的值。我写下面的代码,它完全正常工作:
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());
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
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.
如果有更好的解决方案,我希望你们帮助我。我可以不遍历整个json对象(直到找到相关字段)吗?如果json树结构发生更改,此解决方案将无法工作,它需要是静态的,因为此代码的成功方案。
Please suggest better solution.
请建议更好的解决方案
2 个解决方案
#1
1
If you're flexible on what library to use, maybe the JsonPath will be useful for you.
如果您对要使用的库有灵活性,那么JsonPath可能对您有用。
You can update all "elements" with "rdKey" using the following code:
您可以使用以下代码使用“rdKey”更新所有“元素”:
JsonPath.parse(json).set("$..elements[?(@.rdKey)].rdKey", "CircuitID(FullPartial)").json()
#2
1
If you want to escape traversing of JSON then you can use JSONPointer
, available in same org.json
library. E.g.:
如果要转义JSON的遍历,则可以使用JSONPointer,它可以在同一个org.json库中使用。例如。:
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 Pointer query language can be referred in:
JSON指针查询语言可以参考:
https://tools.ietf.org/html/rfc6901
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指针非常基本,它不支持通配符。所以你需要确定元素名称,否则会抛出异常。
#1
1
If you're flexible on what library to use, maybe the JsonPath will be useful for you.
如果您对要使用的库有灵活性,那么JsonPath可能对您有用。
You can update all "elements" with "rdKey" using the following code:
您可以使用以下代码使用“rdKey”更新所有“元素”:
JsonPath.parse(json).set("$..elements[?(@.rdKey)].rdKey", "CircuitID(FullPartial)").json()
#2
1
If you want to escape traversing of JSON then you can use JSONPointer
, available in same org.json
library. E.g.:
如果要转义JSON的遍历,则可以使用JSONPointer,它可以在同一个org.json库中使用。例如。:
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 Pointer query language can be referred in:
JSON指针查询语言可以参考:
https://tools.ietf.org/html/rfc6901
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指针非常基本,它不支持通配符。所以你需要确定元素名称,否则会抛出异常。