使用不带字符串名称的getJSONArray

时间:2013-07-22 20:34:06

标签: android json

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

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

当我使用getJSONArray(“subcat_1”)时它正在工作但是,我想获得这些字符串而不需要将它们预先保存在数组{subcat_1,subcat_2,...}中以维护动态功能?< / p>

1 个答案:

答案 0 :(得分:2)

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

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