0

from the following link , I need to get the value stored in "media".

Here is my code:

Content = Content.replace("jsonFlickrFeed(", "");
Content = Content.replace("})", "}");
jsonResponse = new JSONObject(Content);
JSONArray jsonMainNode = jsonResponse.optJSONArray("items"); // this works great!

But I can not access past "items"

3 Answers 3

1

You will have to loop through the JSON like this:

...
JSONArray jsonMainNode = jsonResponse.optJSONArray("items");

for (int i=0; i<jsonMainNode.length(); i++) {

    String media = jsonMainNode.getJSONObject(i).getString("media");
}

This will loop through the images and return the value(s) in media.

In your case it should be something like this:

..
JSONArray jsonMainNode = jsonResponse.optJSONArray("items");

for (int i=0; i<jsonMainNode.length(); i++) {

    JSONObject finalNode = jsonMainNode.getJSONObject(i);
    JSONArray finalArray = finalNode.optJSONArray("media");

    for (int j=0; j<finalArray.length(); j++) {
        String m = finalArray.getJSONObject(j).getString("m");
    }

}

...because there is another node inside the media node, called m.

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

2 Comments

and how do i get that "m" stuff out of there ?
You can try looping through media in the for loop and then use jsonMainNode.getJSONObject(i).getString("m"); or something.
1

Here is an example getting the tags string for each item in the JSONArray that you have:

for (int i = 0; i < jsonMainNode.length(); i++){
    JSONObject jsonObject = jsonMainNode.getJSONObject(i);
    String tags = jsonObject.getString("tags");
}

This will iterate through all JSONObjects that are in the array, and extract the tags field from each object.

2 Comments

jsonMainNode.get(i); gave me an error: Type mismatch: cannot convert from Object to JSONObject
My mistake, use getJSONObject
0
JSONArray jsonMainNode = jsonResponse.optJSONArray("items"); // this works great!
            for (int i = 0; i < jsonMainNode.length(); i++) {
                JSONObject item = (JSONObject) jsonMainNode.get(i);
                JSONObject media = item.getJSONObject("media");
                String valueMedia = media.getString("m");
                Log.d("TAG", valueMedia);
            }

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.