逐个读取JsonArray中的元素。

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

Reading element one by one in JsonArray

问题

我通过API调用得到了以下JSON。

String jsonString = "{\r\n" + 
    "\"count\": 100,\r\n" + 
    "\"limit\": 100,\r\n" + 
    "\"totalResults\": 225,\r\n" + 
    "\"hasMore\": true,\r\n" + 
    "\"items\": [\r\n" + 
    "    {\r\n" + 
    "        \"id\": \"55D40522-8672-48B0-B225-FD3CE6686AD6\",\r\n" + 
    "        \"name\": \"AcmeHCMExtended\",\r\n" + 
    "        \"version\": \"20.01.01.03\",\r\n" + 
    "        \"status\": \"DRAFT\"\r\n" + 
    "    },\r\n" + 
    "    {\r\n" + 
    "        \"id\": \"2DB4C83B-0CF9-4A8E-AC41-A29B30324121\",\r\n" + 
    "        \"name\": \"AFinancialBot\",\r\n" + 
    "        \"version\": \"1.0\",\r\n" + 
    "        \"status\": \"DRAFT\"\r\n" + 
    "    },\r\n" + 
    "    {\r\n" + 
    "        \"id\": \"7EA85B81-3CA1-4BE0-B095-217E7C93DCEF\",\r\n" + 
    "        \"name\": \"AIAMFG\",\r\n" + 
    "        \"version\": \"1.0\",\r\n" + 
    "        \"status\": \"DRAFT\"\r\n" + 
    "    }" +
    "]}";

我想逐个读取items下的name。我正在使用以下代码。

JsonObject obj = Json.parse(jsonString).asObject();
JsonArray items = obj.get("items").asArray();
System.out.println(items);   // 它会打印items下的所有内容。
// 下面这行代码会报错
// System.out.println(items.name);

我无法遍历item数组,它不显示数组的长度属性。

你能指导我如何逐个访问元素吗?

英文:

I have below JSON returned by API call.

String jsonString="{\r\n" + 
		"    \"count\": 100,\r\n" + 
		"    \"limit\": 100,\r\n" + 
		"    \"totalResults\": 225,\r\n" + 
		"    \"hasMore\": true,\r\n" + 
		"    \"items\": [\r\n" + 
		"        {\r\n" + 
		"            \"id\": \"55D40522-8672-48B0-B225-FD3CE6686AD6\",\r\n" + 
		"            \"name\": \"AcmeHCMExtended\",\r\n" + 
		"            \"version\": \"20.01.01.03\",\r\n" + 
		"            \"status\": \"DRAFT\"\r\n" + 
		"        },\r\n" + 
		"        {\r\n" + 
		"            \"id\": \"2DB4C83B-0CF9-4A8E-AC41-A29B30324121\",\r\n" + 
		"            \"name\": \"AFinancialBot\",\r\n" + 
		"            \"version\": \"1.0\",\r\n" + 
		"            \"status\": \"DRAFT\"\r\n" + 
		"        },\r\n" + 
		"        {\r\n" + 
		"            \"id\": \"7EA85B81-3CA1-4BE0-B095-217E7C93DCEF\",\r\n" + 
		"            \"name\": \"AIAMFG\",\r\n" + 
		"            \"version\": \"1.0\",\r\n" + 
		"            \"status\": \"DRAFT\"\r\n" + 
		"        }"

I want to read name one by one under the itmes. I am using below code.

JsonObject obj=Json.parse(jsonString).asObject();
 JsonArray items=obj.get("items").asArray();
System.out.println(items);   //it prints everything under items.
`System.out.println(items.name); `  //It gives error

I am not able to iterate item array. it does not show length property of array.

Can you guide me how can I access element one by one.

答案1

得分: 1

看起来你正在使用javax.json.JsonArray的API或类似的内容。如果是这样的话:

JsonArray items = obj.get("items").asArray();

for (int i = 0; i < items.size(); i++) {
    System.out.println(items.getJsonObject(i));
}

请注意,JsonArray实现了java.util.List,而你正在使用标准的ListAPI方法来获取大小。

相同的代码也可以用于com.google.gson.JsonArray……尽管这个类并没有实现List。或者,你可以利用com.google.gson.JsonArray实现了Iterable接口,从而使用“for each”循环:

for (JsonElement e : items) {
    System.out.println(e);
}

对于org.json.JSONArray,等效的代码使用length()而不是size()

在所有情况下,你应该能够通过搜索并阅读你正在使用的JSON库的javadocs来自己找到答案。

更新 - 显然你正在使用com.eclipsesource.minimal-json。根据这个javadoc,它有一个size()方法并且实现了Iterable接口。

英文:

It looks like you are using the javax.json.JsonArray APIs or something similar. If so:

JsonArray items = obj.get(&quot;items&quot;).asArray();

for (int i = 0; i &lt; items.size(); i++) {
    System.out.println(items.getJsonObject(i));
}

Note that JsonArray implements java.util.List and you are getting the size using the standard List API method.

The same code would work with com.google.gson.JsonArray ... though this class does NOT implement List. Alternatively, you could make use of the fact that com.google.gson.JsonArray implements Iterable and write a "for each" loop:

for (JsonElement e: items) {
    System.out.println(e);
}

The equivalent code for org.json.JSONArray uses length() instead of size().


In all cases, you should be able to answer your own question by searching for and reading the javadocs for the JSON library that you are using.


UPDATE - Apparently you are using the com.eclipsesource.minimal-json. According to this javadoc, it has a size() method and implements Iterable.

huangapple
  • 本文由 发表于 2020年6月29日 18:29:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/62636180.html
匿名

发表评论

匿名网友

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

确定