Uzer
Uzer

Reputation: 187

Incorrect JSONException

I'm trying to use JSON objects from the Guardian API.

This is the result from my call:

https://pastebin.com/wqggLEeZ

This is my code

JSONObject root = new JSONObject(jsonData);
JSONArray resultArray = root.getJSONObject("response").getJSONArray("results");

for(int i=0;i<resultArray.length();i++) {
    JSONObject resultElement = resultArray.getJSONObject(i);
    JSONObject blocksElement = resultElement.getJSONObject("blocks");
    JSONObject mainElement = blocksElement.getJSONObject("main");
    JSONArray elementsArray = mainElement.getJSONArray("elements");
    JSONObject elementsElement = elementsArray.getJSONObject(0);
    JSONArray assetsArray = elementsElement.getJSONArray("assets");
    JSONObject assetsElement = assetsArray.getJSONObject(0);

    String imageUrl = assetsElement.getString("file");
    String articleTitle = resultElement.getString("webTitle");

    news.add(new NewsList(articleTitle, imageUrl));
}

The code works fine except that it stops at 3 elements (i=2)

I tried replacing the imageUrl with "test" string in

news.add(new NewsList(articleTitle, imageUrl));

but it still stops at 3 elements.

But when I comment out the part where it finds the imageUrl the whole code works and gives me 10 results like it should do:

JSONObject root = new JSONObject(jsonData);
JSONArray resultArray = root.getJSONObject("response").getJSONArray("results");

for(int i=0;i<resultArray.length();i++){
    JSONObject resultElement = resultArray.getJSONObject(i);
    /*
    JSONObject blocksElement = resultElement.getJSONObject("blocks");
    JSONObject mainElement = blocksElement.getJSONObject("main");
    JSONArray elementsArray = mainElement.getJSONArray("elements");
    JSONObject elementsElement = elementsArray.getJSONObject(0);
    JSONArray assetsArray = elementsElement.getJSONArray("assets");
    JSONObject assetsElement = assetsArray.getJSONObject(0);
    String imageUrl = assetsElement.getString("file");
    */

    String articleTitle = resultElement.getString("webTitle");

    news.add(new NewsList(articleTitle, "test"));
}

I've looked around in Android monitor and it seems like it has a problem that says

org.json.JSONException: No value for main

But that is not correct as the URL for the first 3 result is found without any problem, the problem only occurs after 3 iterations of the loop and I can't find any reason for why this is happening.

Upvotes: 1

Views: 69

Answers (2)

Mr. Mad
Mr. Mad

Reputation: 1238

this is happening because of there is no field "main" in the response at some position in the line

            JSONObject mainElement = blocksElement.getJSONObject("main");

so if at any of the position any of the fields are missing from the response, then after that position the code will not compile in JSON parsing.

sorry for bad english .

Upvotes: 1

Sergio
Sergio

Reputation: 30655

Use everywhere "opt" instead of "get', e.g.:

JSONObject assetsElement = assetsArray.optJSONObject(0);
String imageUrl = assetsElement.optString("file");

Upvotes: 1

Related Questions