Get json array keys in android

前端 未结 5 1164
渐次进展
渐次进展 2020-12-15 09:16
{
    \"204\": {
        \"host\": \"https:\\/\\/abc.com\\/production-source\\/ChangSha\\/2013\\/12\\/02\\/0\\/0\\/A\\/Content\\/\",
        \"timestamp\": 138590988         


        
5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-15 09:21

    User this method to iterate json dynamically

    private void parseJson(JSONObject data) {
    
            if (data != null) {
                Iterator it = data.keys();
                while (it.hasNext()) {
                    String key = it.next();
    
                    try {
                        if (data.get(key) instanceof JSONArray) {
                            JSONArray arry = data.getJSONArray(key);
                            int size = arry.length();
                            for (int i = 0; i < size; i++) {
                                parseJson(arry.getJSONObject(i));
                            }
                        } else if (data.get(key) instanceof JSONObject) {
                            parseJson(data.getJSONObject(key));
                        } else {
                            System.out.println("" + key + " : " + data.optString(key));
                        }
                    } catch (Throwable e) {
                        System.out.println("" + key + " : " + data.optString(key));
                        e.printStackTrace();
    
                    }
                }
            }
        }
    

提交回复
热议问题