gpt4 book ai didi

android - 使用没有字符串名称的 getJSONArray

转载 作者:行者123 更新时间:2023-11-29 00:28:16 24 4
gpt4 key购买 nike

我正在尝试解析以下结构中的 JSON 文件

{
"category": {
"subcat_1": [
{
"item1": "xx"
},
{
"item2": "xx"
}
],
"subcat_2": [
{
"item1": "xx"
},
{
"item2": "xx"
}
]
}
}

当我使用 getJSONArray("subcat_1") 时它可以工作,但是,我想获取这些字符串而不需要将它们预先保存在数组 { subcat_1, subcat_2, ... } 中以保持动态特性?

最佳答案

您可以使用 keys() 获取对象的键,它返回一个迭代器:

Iterator it = json.keys();
while(it.hasNext()) {
String key = (String)it.next();
JSONObject current = json.getJSONObject(key);
// do something with current
}

关于android - 使用没有字符串名称的 getJSONArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17796875/

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