在json中解析一个对象数组

时间:2016-02-21 05:49:05

标签: android json

我有一个以下的json对象,我能够得到plistDatepcategory,但我想知道如何解析以下json中的每个image对象。

这是我到目前为止所拥有的

obj = new JSONObject(jsonObject);
listImages = obj.optString("images");

的JSONObject

{
    "plistDate": "2016-02-19 22:02:41",
    "pcategory": "Kategori seciniz",
    "images": {
        "pimagePath": "products/138_1455940961.jpg",
        "pimagePath2": "products/138_1455940961_2.jpg",
        "pimagePath3": "products/138_1455940961_3.jpg",
        "pimagePath4": "products/138_1455940961_4.jpg",
        "pimagePath5": "products/138_1455940961_5.jpg"
    }
}

1 个答案:

答案 0 :(得分:1)

images是内部对象,所以你必须像那样检索它

    JSONObject  obj = new JSONObject("you jsin String");
    String pcategory = obj.getString("pcategory");
    String plistDate = obj.getString("plistDate");

    JSONObject images_JsonObject =  obj.getJSONObject("images");
    String pimagePath = images_JsonObject.getString("pimagePath");
    String pimagePath2 = images_JsonObject.getString("pimagePath2");
    String pimagePath3 = images_JsonObject.getString("pimagePath3");
    String pimagePath4 = images_JsonObject.getString("pimagePath4");
    String pimagePath5 = images_JsonObject.getString("pimagePath5");
  

更新

        JSONObject images_JsonObject =  obj.getJSONObject("images");

        Iterator<String> stringIterable =  images_JsonObject.keys();
        HashMap<String, String> hashMap = new HashMap<>();
        ArrayList<String> list = new ArrayList<>();            

        while (stringIterable.hasNext()){
            String key = stringIterable.next();
            hashMap.put(key, images_JsonObject.getString(key));
            list.add(images_JsonObject.getString(key));
        }