0

I'm trying to parse a JSON which contains:

"images": [                 
    "https://d1wgio6yfhqlw1.cloudfront.net/sysimages/product/resized6/Interior_St_Pauls_Cathedral_132_12992.jpg",
    "https://d1kioxk2jrdjp.cloudfront.net/resized/486x324/48-st_pauls_ctahedral_millenirm_bridge.jpg",
    "http://i4.mirror.co.uk/incoming/article8299330.ece/ALTERNATES/s615b/LOND-2016-052-HMQ-St-Pauls-Thanks-Giving-704JPG.jpg"
            ]

The problem is that I don't know how to get the string of elements inside the "images" node

My code currently looks like:

if(c.has("images") && !c.isNull("images")){
    JSONArray imagenes = jsonObj.getJSONArray("images");
    for (int j = 0; j < imagenes.length(); j++) {
        JSONObject m = imagenes.getJSONObject(i);


    }
}

How can I get each string in the array without using a "key"?
I don't know what to do with "M" next.

0

2 Answers 2

3

You want a string, not an object. Strings don't have nested keys.

Use getString(i) method instead of getJSONObject(i)

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! That will make it!
this knowledge helped me... I think that in json array only we can get json objects.......:)
1

Your images array contains with string values not with json object. So you need to get string instead of jsonObject.

if(c.has("images") && !c.isNull("images")){
    JSONArray imagenes = jsonObj.getJSONArray("images");
    for (int j = 0; j < imagenes.length(); j++) {
        String imgURL = imagenes.optString(i);

        System.out.println(imgURL);   
    }
}

Output:

https://d1wgio6yfhqlw1.cloudfront.net/sysimages/product/resized6/Interior_St_Pauls_Cathedral_132_12992.jpg

https://d1kioxk2jrdjp.cloudfront.net/resized/486x324/48-st_pauls_ctahedral_millenirm_bridge.jpg

http://i4.mirror.co.uk/incoming/article8299330.ece/ALTERNATES/s615b/LOND-2016-052-HMQ-St-Pauls-Thanks-Giving-704JPG.jpg

Comments

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.