解析 Java 中包含多个 JSON 数组的 API 响应。

huangapple go评论78阅读模式
英文:

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>

&quot;root&quot;: {
	&quot;name&quot;: &quot;Test&quot;,
	&quot;version&quot;: 1,
	&quot;type&quot;: &quot;process&quot;,
	&quot;steps&quot;: [{
		&quot;name&quot;: &quot;Level 1&quot;,
		&quot;origin&quot;: &quot;Origin1&quot;,
		&quot;type&quot;: &quot;step&quot;,
		&quot;state&quot;: &quot;complete&quot;,
		&quot;steps&quot;: []
	}, {
		&quot;name&quot;: &quot;Level 2&quot;,
		&quot;origin&quot;: &quot;Origin2&quot;,
		&quot;type&quot;: &quot;step&quot;,
		&quot;state&quot;: &quot;complete&quot;,
		&quot;steps&quot;: []
	}, {
		&quot;name&quot;: &quot;Level 3&quot;,
		&quot;origin&quot;: &quot;Origin3&quot;,
		&quot;type&quot;: &quot;process&quot;,
		&quot;state&quot;: &quot;error&quot;,
		&quot;steps&quot;: [{
			&quot;name&quot;: &quot;Level 11&quot;,
			&quot;origin&quot;: &quot;origin 11&quot;,
			&quot;type&quot;: &quot;step&quot;,
			&quot;state&quot;: &quot;complete&quot;,
			&quot;steps&quot;: []
		}, {
			&quot;name&quot;: &quot;Level 12&quot;,
			&quot;origin&quot;: &quot;origin 12&quot;,
			&quot;type&quot;: &quot;step&quot;,
			&quot;state&quot;: &quot;complete&quot;,
			&quot;operation&quot;: &quot;fulfill&quot;,
			&quot;steps&quot;: []
		}, {
			&quot;name&quot;: &quot;Level 13&quot;,
			&quot;origin&quot;: &quot;origin 13&quot;,
			&quot;type&quot;: &quot;process&quot;,
			&quot;state&quot;: &quot;error&quot;,
			&quot;steps&quot;: [{
					&quot;name&quot;: &quot;Level 111&quot;,
					&quot;origin&quot;: &quot;origin 111&quot;,
					&quot;type&quot;: &quot;step&quot;,
					&quot;state&quot;: &quot;complete&quot;,
					&quot;steps&quot;: []
				},
				{
					&quot;name&quot;: &quot;Level 112&quot;,
					&quot;origin&quot;: &quot;Origin 112&quot;,
					&quot;type&quot;: &quot;process&quot;,
					&quot;state&quot;: &quot;complete&quot;,
					&quot;steps&quot;: [{
						&quot;name&quot;: &quot;Level 1111&quot;,
						&quot;origin&quot;: &quot;origin 1111&quot;,
						&quot;type&quot;: &quot;step&quot;,
						&quot;state&quot;: &quot;complete&quot;,
						&quot;steps&quot;: []
					}]
				}
			]
		}]
	}]
}

</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&lt;arg.length(); i++) {
		System.out.println(arg.getJSONObject(i).getString(&quot;name&quot;));
		System.out.println(arg.getJSONObject(i).getString(&quot;origin&quot;));
		parse (arg.getJSONObject(i).getJSONArray(&quot;steps&quot;));
	}
}

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(&quot;root&quot;);
parse(jo_root.getJSONArray(&quot;steps&quot;));

I am sure you know how to only print the ones you want from here.

huangapple
  • 本文由 发表于 2020年8月6日 04:12:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63272864.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定