gpt4 book ai didi

java - 从 JSONArray 中检索特定元素

转载 作者:太空宇宙 更新时间:2023-11-04 07:46:18 25 4
gpt4 key购买 nike

如何从以下内容中检索“B2”? JSONArray?

        JSONArray o = new JSONArray();
for(int i = 0; i < 3; i++) {
List<Object> list = new ArrayList<Object>();
list.add("B" + i);
list.add(100+i);
o.put(list);
}

我认为它类似于以下内容,尽管这根本不正确。

o.get(2)[0]

最佳答案

问题是您正在使用 put() ,其中:

Put a value in the JSONArray, where the value will be a JSONArray which is produced from a Collection.

因此,这会将一个数组添加到您的数组中。像这样的事情:

[
[
"B1", "B2"
]
]

如果这是有意的,那么您首先需要获取内部数组,然后是第二个条目:

o.getJSONArray(0).getInt(1);

否则,如果您想使用 List 中的条目填充现有的 JSONArray o,则需要使用 JSONArray(Collection)-constructor .

关于java - 从 JSONArray 中检索特定元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15236248/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com