如何Android JSON多数组解析

时间:2015-08-11 06:00:00

标签: android arrays json parsing

通过适用于Android的Json数据服务器API可以看到这项工作。 对于一般的Json格式,它可以很好地显示数据。 但是,在安排多JSON的情况下,它可能不会显示数据。

我有一个类似的JSON:

  {
    "RESULT": "1",
    "ERROR": "",
    "UNIFORM": [
        {
            "UNIFORMINFO": [
                {
                    "UniformInventoryID": "29",
                    "UniformName": "생활복",
                    "UniformCreated": "2015",
                    "SeasonType": "2",
                    "Sex": "0",
                    "Size": "L",
                    "UniformImage": "URL",
                    "UniformStock": "147"
                },
                {
                    "UNIFORMBYCOST": [
                        {
                            "CostName": "단품가",
                            "UniformCost": "30000"
                        },
                        {
                            "CostName": "세트가",
                            "UniformCost": "20000"
                        }
                    ]
                }
            ]
        },
        {
            "UNIFORMINFO": [
                {
                    "UniformInventoryID": "28",
                    "UniformName": "생활복",
                    "UniformCreated": "2015",
                    "SeasonType": "2",
                    "Sex": "0",
                    "Size": "M",
                    "UniformImage": "URL",
                    "UniformStock": "147"
                },
                {
                    "UNIFORMBYCOST": [
                        {
                            "CostName": "단품가",
                            "UniformCost": "35000"
                        },
                        {
                            "CostName": "세트가",
                            "UniformCost": "25000"
                        }
                    ]
                }
            ]
        },
        {
            "UNIFORMINFO": [
                {
                    "UniformInventoryID": "26",
                    "UniformName": "생활복",
                    "UniformCreated": "2015",
                    "SeasonType": "2",
                    "Sex": "1",
                    "Size": "M",
                    "UniformImage": "URL",
                    "UniformStock": "147"
                },
                {
                    "UNIFORMBYCOST": [
                        {
                            "CostName": "단품가",
                            "UniformCost": "50000"
                        },
                        {
                            "CostName": "세트가",
                            "UniformCost": "40000"
                        }
                    ]
                }
            ]
        }
    ]
}

所以我尝试在Android中解决:

 private void LISTUNIFORM_RESULT() {
        try {
            JSONObject json_ListUniform = new JSONObject(_LISTUNIFORM_RESPONSE);

            if(json_ListUniform.getString("RESULT").equals("1")) {
                JSONArray UNIFORM_ARRAY = json_ListUniform.getJSONArray("UNIFORM");

                for(int i = 0; i < UNIFORM_ARRAY.length(); i++) {
                    JSONObject UNIFORM_OBJ = UNIFORM_ARRAY.getJSONObject(i);
                    JSONArray UNIFORMINFO_ARRAY = UNIFORM_OBJ.getJSONArray("UNIFORMINFO");

                    for(int j = 0; j < UNIFORMINFO_ARRAY.length(); j++) {
                        JSONObject UNIFOMINFO_OBJ = UNIFORMINFO_ARRAY.getJSONObject(j);

                        UniformInventoryID = UNIFORMINFO_ARRAY.getJSONObject(j).getString("UniformInventoryID");

                        JSONArray UNIFORMBYCOST_ARRAY = UNIFOMINFO_OBJ.getJSONArray("UNIFORMBYCOST");

                        for(int z = 0; z < UNIFORMBYCOST_ARRAY.length(); z++) {
                            CostName = UNIFORMBYCOST_ARRAY.getJSONObject(z).getString("CostName");
                            UniformCost= UNIFORMBYCOST_ARRAY.getJSONObject(z).getString("UniformCost");

                        }
                    }
                }
            }
        } catch (JSONException e) {
            Log.d("MIPOS.IO", "JSON Result ` Exception : " + e.toString());
        }
    }

但它没有解决。

08-11 14:29:51.200    8162-8162/? D/MIPOS.IO﹕ JSON Result ` Exception : org.json.JSONException: No value for UNIFORMBYCOST

如果解析这些json的多个数组,我该怎么办?

1 个答案:

答案 0 :(得分:0)

“UNIFORMINFO”是一个有两个元素的数组,一个有一些细节和一些。另一个是数组。 对于j = 0,你不能使用getJSONArray(“UNIFORMBYCOST”),因为在索引0处没有这样的数组。 修改您的代码,如:

JSONObject UNIFOMINFO_OBJ = UNIFORMINFO_ARRAY.getJSONObject(0);

                    UniformInventoryID = UNIFORMINFO_ARRAY.getJSONObject(j).getString("UniformInventoryID");

                    JSONArray UNIFORMBYCOST_ARRAY = UNIFORMINFO_ARRAY.getJSONObject(1).getJSONArray("UNIFORMBYCOST");

                    for(int z = 0; z < UNIFORMBYCOST_ARRAY.length(); z++) {
                        CostName = UNIFORMBYCOST_ARRAY.getJSONObject(z).getString("CostName");
                        UniformCost= UNIFORMBYCOST_ARRAY.getJSONObject(z).getString("UniformCost");

                    }

不要使用for循环j。