0

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"
    }
}
3
  • json.images.pimagepath, json.images.pimagepath2... and so on Commented Feb 21, 2016 at 5:58
  • Please check my updated question having a sample code. Commented Feb 21, 2016 at 6:00
  • 1
    codex2android.blogspot.in/2015/11/… Commented Feb 21, 2016 at 6:54

1 Answer 1

1

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));
        }
Sign up to request clarification or add additional context in comments.

4 Comments

how should I check if ImagePath5 exist in the json?
@texas, in case if you are not sure about the how many and what is the keys are present in the JSON Object, use Iterator and save it in HashMap<String, String>
could u please illustrate in your answer ?
@texas, It seems you already have the answer to that question, How may I help you?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.