0

I tough i knew the way to parse a JSON but when I try to access to the key where the value i want is Android Studio gives me the following message:

org.JSON.JSONEXCEPTION: no value for large

As you can see it says that there is no value for the key "large" but clearly it is, anyway, this is my json parse method

 public static List<News> parseJSONtoNews(JSONArray jsonArray) throws JSONException {
    List<News> newsList = new ArrayList<>();

    for (int i = 0; i < jsonArray.length(); i++) {
        News news = new News();
        JSONObject jsonObjectNews = jsonArray.getJSONObject(i);

        JSONArray jsonArrayCategories = jsonObjectNews.getJSONArray(JSONKeys.KEY_CATEGORIES);


        int category = getCatgories(jsonArrayCategories);
        news.setCategories(category);

        news.setiD(jsonObjectNews.getInt("id"));

        JSONObject jsonObjectTitle = jsonObjectNews.getJSONObject("title");
        news.setTitle(jsonObjectTitle.getString("rendered"));

        JSONObject jsonObjectContent = jsonObjectNews.getJSONObject("content");
        news.setContent(jsonObjectContent.getString("rendered"));

        JSONObject jsonObjectImage = jsonObjectNews.getJSONObject("better_featured_image");
        JSONObject jsonObjectMediaDetails = jsonObjectImage.getJSONObject("media_details");
        JSONObject jsonObjectSizes = jsonObjectMediaDetails.getJSONObject("sizes");
        JSONObject jsonObjectMediumLarge = jsonObjectSizes.getJSONObject("large");
        news.setImageURL(jsonObjectMediumLarge.getString("source_url"));
        newsList.add(news);
    }



    return newsList;
}

public static int getCatgories(JSONArray jsonArray) throws JSONException{
    int categories = 0;
    for (int i = 0; i<jsonArray.length(); i++){
        categories = jsonArray.getInt(i);
    }

    return categories;
}

This is the JSON I want to Parse

[
  {
    "id": 742,
        "title": {
      "rendered": “title”
    },
    "content": {
      "rendered": “content”
    },
    "categories": [
      4
    ],
    "tags": [],
    "better_featured_image": {
        "file": "2016/05/20160520_191324.jpg",
        "sizes": {
          "thumbnail": {
            "file": "20160520_191324-150x150.jpg",
            "source_url": "http://www.aarc.com.mx/wp-content/uploads/2016/05/20160520_191324-150x150.jpg"
          },
          "medium": {
            "file": "20160520_191324-300x169.jpg",
            "source_url": "http://www.aarc.com.mx/wp-content/uploads/2016/05/20160520_191324-300x169.jpg"
          },
          "medium_large": {
            "file": "20160520_191324-768x432.jpg",
            "source_url": "http://www.aarc.com.mx/wp-content/uploads/2016/05/20160520_191324-768x432.jpg"
          },
          "large": {
            "file": "20160520_191324-1024x576.jpg",
            "source_url": "http://www.aarc.com.mx/wp-content/uploads/2016/05/20160520_191324-1024x576.jpg"
          }
        },
      "post": 742,
      "source_url": "http://www.aarc.com.mx/wp-content/uploads/2016/05/20160520_191324.jpg"
    },
 },
{
    "id": 745,
        "title": {
      "rendered": “title”
    },
    "content": {
      "rendered": “content”
    },
    "categories": [
      4
    ],
    "tags": [],
    "better_featured_image": {
        "file": "2016/05/20160520_191324.jpg",
        "sizes": {
          "thumbnail": {
            "file": "20160520_191324-150x150.jpg",
            "source_url": "http://www.aarc.com.mx/wp-content/uploads/2016/06/20160520_191324-150x150.jpg"
          },
          "medium": {
            "file": "20160520_191324-300x169.jpg",
            "source_url": "http://www.aarc.com.mx/wp-content/uploads/2016/06/20160520_191324-300x169.jpg"
          },
          "medium_large": {
            "file": "20160520_191324-768x432.jpg",
            "source_url": "http://www.aarc.com.mx/wp-content/uploads/2016/06/20160520_191324-768x432.jpg"
          },
          "large": {
            "file": "20160520_191324-1024x576.jpg",
            "source_url": "http://www.aarc.com.mx/wp-content/uploads/2016/06/20160520_191324-1024x576.jpg"
          }
        },
      "post": 742,
      "source_url": "http://www.aarc.com.mx/wp-content/uploads/2016/06/20160520_191324.jpg"
    },
   }
]

As you can see there exist the key "large" but i cant access to it, it works with the "thumbnail" key, so I dont know how to hanlde this, i hope you guys could help me! Thanks

1

1 Answer 1

2

It looks to me:

JSONObject jsonObjectImage = jsonObjectNews.getJSONObject("better_featured_media");
JSONObject jsonObjectMediaDetails = jsonObjectImage.getJSONObject("media_details");

jsonObjectMediaDetails shouldn't exist. I don't see media_details anywhere in the JSON. I also don't see better_featured_media. I think it should be better_featured_image.

Try:

JSONObject jsonObjectImage = jsonObjectNews.getJSONObject("better_featured_image");
JSONObject jsonObjectSizes = jsonObjectImage.getJSONObject("sizes");
Sign up to request clarification or add additional context in comments.

4 Comments

I am sorry but i have all of those keys in a class called JSONKEYS, I wrote it wrong but in my code it says ""better_featured_image", as I told you i can access to the htumbnail key but i cant to the large one, (which is the one that i want) thank you for reply, i appreciate it
@miguelacio it still looks to me that you are making one extra call you don't need: the getJsonObject("media_details"). that key doesn't exist in the JSON you posted. Try adding a breakpoint in that function, and stepping through each statement and checking the value at each step.
@miguelacio I think Kevin is right, I've also observed the code and 'media_details' key doesn't exist. remove that one and try without it.
Miguel please do try this. It has to be the correct answer

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.