英文:
Parsing JSON response from api using Java containing multiple JSON arrays
问题
以下是您所需的JSON中的名称和来源信息:
- Level 1 的名称是 "Level 1",来源是 "Origin1"。
- Level 11 的名称是 "Level 11",来源是 "origin 11"。
- Level 111 的名称是 "Level 111",来源是 "origin 111"。
- Level 1111 的名称是 "Level 1111",来源是 "origin 1111"。
英文:
I have the following JSON from an API response. I need capture the name & origin for Level 1, Level 11, Level 111 & Level 1111. This might increase or decrease based on the order we place in the application. When the number of levels are fixed, i am able to parse and get the name & origin using JSON parser, but if this keep changing how do i address that. JSON response below -
<i>
"root": {
"name": "Test",
"version": 1,
"type": "process",
"steps": [{
"name": "Level 1",
"origin": "Origin1",
"type": "step",
"state": "complete",
"steps": []
}, {
"name": "Level 2",
"origin": "Origin2",
"type": "step",
"state": "complete",
"steps": []
}, {
"name": "Level 3",
"origin": "Origin3",
"type": "process",
"state": "error",
"steps": [{
"name": "Level 11",
"origin": "origin 11",
"type": "step",
"state": "complete",
"steps": []
}, {
"name": "Level 12",
"origin": "origin 12",
"type": "step",
"state": "complete",
"operation": "fulfill",
"steps": []
}, {
"name": "Level 13",
"origin": "origin 13",
"type": "process",
"state": "error",
"steps": [{
"name": "Level 111",
"origin": "origin 111",
"type": "step",
"state": "complete",
"steps": []
},
{
"name": "Level 112",
"origin": "Origin 112",
"type": "process",
"state": "complete",
"steps": [{
"name": "Level 1111",
"origin": "origin 1111",
"type": "step",
"state": "complete",
"steps": []
}]
}
]
}]
}]
}
</i>
答案1
得分: 0
Assuming you are using org.json, you can traverse the entire tree using a method like this one:
public static void parse(JSONArray arg) {
for (int i = 0; i < arg.length(); i++) {
System.out.println(arg.getJSONObject(i).getString("name"));
System.out.println(arg.getJSONObject(i).getString("origin"));
parse(arg.getJSONObject(i).getJSONArray("steps"));
}
}
From the code, you can see that I am only displaying name and Origin. There is a recursive call that traverses each steps array. Once you get the initial object object (assuming you are using the variable response), you can then just call the parse method:
JSONObject jo = new JSONObject(response);
JSONObject jo_root = jo.getJSONObject("root");
parse(jo_root.getJSONArray("steps"));
I am sure you know how to only print the ones you want from here.
英文:
Assuming you are using org.json, you can traverse the entire tree using a method like this one:
public static void parse(JSONArray arg) {
for (int i = 0; i<arg.length(); i++) {
System.out.println(arg.getJSONObject(i).getString("name"));
System.out.println(arg.getJSONObject(i).getString("origin"));
parse (arg.getJSONObject(i).getJSONArray("steps"));
}
}
From the code, you can see that I am only displaying name and Origin. There is a recursive call the traverses each steps array. Once you get the initial object object (assuming you are using the variable response), you can then just call the parse method:
JSONObject jo = new JSONObject(response);
JSONObject jo_root = jo.getJSONObject("root");
parse(jo_root.getJSONArray("steps"));
I am sure you know how to only print the ones you want from here.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论