2

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

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

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

4

1 回答 1

2

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

Iterator it = json.keys();
while(it.hasNext()) {
  String key = (String)it.next();
  JSONObject current = json.getJSONObject(key);
  // do something with current
}
于 2013-07-22T20:43:14.647 回答