0

I have the following Json. link

I would like to get image_hall_list and image_place_list all url value.
I tried with the following code but no any result.

JSONObject JO = new JSONObject(result);
JSONArray ja = JO.getJSONArray("image_place_list"); //get the array

 for (int i = 0; i < ja.length(); i++) {
    JSONObject jo = null;
    try {
        jo = ja.getJSONObject(i);                    
        jsonurl.add(jo.getString("url"));
    } catch (JSONException e1) {
        e1.printStackTrace();
    }
}

3 Answers 3

2

Try this:

JSONObject JO = new JSONObject(result);
JSONArray ja = JO.getJSONArray("place_list"); //get the array
for (int i = 0; i < ja.length(); i++) {
    JSONObject jo = null;
    try {
        jo = ja.getJSONObject(i);
        JSONArray imageHallList = jo.getJSONArray("image_hall_list");
        for (int j = 0; j < imageHallList.length(); j++) {
            JSONObject oneImageHallList = imageHallList.getJSONObject(j);
            jsonurl.add(oneImageHallList.getString("url"));
        }
        JSONArray imagePlaceList = jo.getJSONArray("image_place_list");
        for (int j = 0; j < imagePlaceList.length(); j++) {
            JSONObject oneImagePlaceList = imagePlaceList.getJSONObject(j);
            jsonurl.add(oneImagePlaceList.getString("url"));
        }
    } catch (JSONException e1) {
        e1.printStackTrace();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

I would recommend some methods.

One to extract all the URLs for a given object.

public ArrayList<String> getURLs(JSONObject jo, String key) throws JSONException {
    List<String> urls = new ArrayList<String>();
    JSONArray arr = jo.getJSONArray(key);
    for (int j = 0; j < arr.length(); j++) {
        JSONObject innerObj = arr.getJSONObject(j);
        urls.add(innerObj.getString("url"));
    }
    return urls;
}

Then, you can use that twice for the respective keys. You also need to first get "place_list" based if your result variable is directly from that link.

try {
    JSONObject jsonResponse = new JSONObject(result);
    JSONArray ja = jsonResponse.getJSONArray("place_list"); //get the array

     for (int i = 0; i < ja.length(); i++) {
        JSONObject jo = ja.getJSONObject(i);                    
        jsonurl.addAll(getURLs(jo, "image_hall_list"));
        jsonurl.addAll(getURLs(jo, "image_place_list"));
    }
} catch (JSONException e1) {
    e1.printStackTrace();
}

Comments

0

Use a nested for-loop. First grab the items before the image_hall_list and image_place_list, and then once you have the values stored, loop through image_hall_list, and image_place_list by getting that JSON object and loop through the elements in the JSON objects.

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.