我正在尝试解析以下结构中的JSON文件
{
"category": {
"subcat_1": [
{
"item1": "xx"
},
{
"item2": "xx"
}
],
"subcat_2": [
{
"item1": "xx"
},
{
"item2": "xx"
}
]
}
}
当我使用getJSONArray(“subcat_1”)时它正在工作但是,我想获得这些字符串而不需要将它们预先保存在数组{subcat_1,subcat_2,...}中以维护动态功能?< / p>
答案 0 :(得分:2)
您可以使用keys()
获取对象的键,它返回一个迭代器:
Iterator it = json.keys();
while(it.hasNext()) {
String key = (String)it.next();
JSONObject current = json.getJSONObject(key);
// do something with current
}