使用改造从JSON字符串解析动态键

时间:2018-10-07 00:17:44

标签: android json retrofit

如何解析此json并遍历每个项目并获取其键字符串和值字符串。(键是动态生成的)

{
"business_industry_type": {
    "11": "dummy1",
    "2": "dummy2",
    "44": "dummy3",
    "4": "dummy4",
    "5": "dummy5",
    "34": "dummy6",
    "7": "dummy7",
    "88": "dummy8",
    "9": "dummy9"

},
"status": "success",
"description": "Successfully ListedType",
"DES_CODE": "NC08"

}

1 个答案:

答案 0 :(得分:0)

也许这会有所帮助,请尝试使用Iterator解决您的问题。

Iterator<?> permisos = jsonObject.keys();
    try {
        while(permisos.hasNext() ){
            String key = (String)permisos.next();

            if(jsonObject.get(key) instanceof JSONObject) {
                Iterator<?> tipo = jsonObject.getJSONObject(key).keys();
                while (tipo.hasNext()) {
                    String key2 = (String) tipo.next();
                    Log.e("TAG", "Key: "+ key2 +" Value: "+jsonObject.getJSONObject(key).getString(key2).toString() );
                }
            }
            else
                Log.e("TAG", "Key: "+ key +" Value: "+jsonObject.getString(key) );
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }