casillas
casillas

Reputation: 16793

Parse an array of object in the json

I have a following json object, I could able to get plistDate, and pcategory, but I wonder how to parse each image object in the following json.

here is what I have so far

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"
    }
}

Upvotes: 0

Views: 47

Answers (1)

Pankaj Nimgade
Pankaj Nimgade

Reputation: 4549

images is inner object so you would have to retrieve it like that

    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");

Update

        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));
        }

Upvotes: 1

Related Questions