Asdemuertes
Asdemuertes

Reputation: 323

JSON parse a JsonObject without key

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.

Upvotes: 0

Views: 2072

Answers (2)

Dharmbir Singh
Dharmbir Singh

Reputation: 17535

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

Upvotes: 1

OneCricketeer
OneCricketeer

Reputation: 191758

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

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

Upvotes: 3

Related Questions