Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

I can't get through the last challenge of "Parsing Data Returned in JSON Format"

No matter what I do, the challenge result is always "Compilation Error! Verify your syntax, especially in the 'for' loop. Remember that the 3 statements in the parenthesis of the 'for' loop are separated by semicolons."

Can anyone please let me know what am I doing wrong?

// JSONObject 'jsonData' was loaded from data.json String name = jsonData.getString("name"); String publisher = jsonData.getString("publisher"); String language = jsonData.getString("language");

JSONArray jsonBooks = jsonData.getJSONArray("books");

for(int i=0;i<jsonBooks.length();i++){ JSONObject jsonBook = jsonBooks.getJSONObject(i); Log.i("CodeChallenge","title"+", "+"pages") }

1 Answer

I answered this previously:

I believe you were at:

Build a Blog Reader Android App > Getting Data from the Web > Parsing Data Returned in JSON Format

Step one:

String name = jsonData.getString("name");
String publisher = jsonData.getString("publisher");
String language  = jsonData.getString("language");

Step two:

JSONArray jsonBooks = jsonData.getJSONArray("books");

Step three:

for(int index=0;index<jsonBooks.length();index++){
        JSONObject jsonObject = jsonBooks.getJSONObject(index);   
        Log.i("CodeChallenge",jsonObject.getString("title")+", "+jsonObject.getInt("pages"));
}

Keep coding!

This lesson is actually flawed for 2 reasons.

  1. It's ambiguous, how I write the Log.i could be via a String or Int for pages. The challenge doesn't specify.

  2. How it's written can be left up to interpretation with several valid instances, all of which fail the analyzer. Is Valid yet fails:

for(int i=0;i< jsonBooks.length();i++)
{
JSONObject book = jsonBooks.getJSONObject(i);
String title = book.getString("title");
Int pages = book.getInt("pages");
Log.i("CodeChallenge",title+", "+pages);
}

for(int i=0;i< jsonBooks.length();i++)
{
JSONObject book = jsonBooks.getJSONObject(i);
String title = book.getString("title");
String pages = book.getString("pages");
Log.i("CodeChallenge",title+", "+pages);
}

And finally your solution can be changed to:

Log.i("CodeChallenge",jsonObject.getString("title")+", "+jsonObject.getString("pages"));

Forgive my formatting, it didn't seem to take here.

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Hi Kunal,

There are actually errors in each of your examples, which is why they fail. The first has a capital "I" for int, and the second declares 'pages' as a String variable. See this other Forum post for more info on that. Your last line has a similar problem about how you are getting "pages" from the JSONObject.

This code challenge has been a little frustrating for students, but it is a very valid example of real life programming headaches. :)

Ah yes, sorry it should be int pages not Int pages. I should've caught that.

Hmm, why is String pages = book.getString("pages"); invalid? Can't we extract a string for the pages?

Ben Jakuben
Ben Jakuben
Treehouse Teacher

It's tough because our Code Challenge engine doesn't give you compile-time errors like a real IDE. Hopefully that's something we can address in the future.

"pages" is actually a number value in the data.json file. As such we can't use the getString() method to parse it. It won't implicitly convert things like that for us; we need to use the specific parsing method (getInt() in this case).

Interesting, this has definitely gotten me a little confused.

When I look at the url: http://blog.teamtreehouse.com/api/get_recent_summary/?count

I see pages as, "pages":148 where 148 is not in quotes. The below however works, not only compile time, but also saves the parsed object to String pages during run time.

JSONObject jsonResponse = new JSONObject(responseData);
String pages = jsonResponse.getString("pages");

This is not supposed to compile?

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Fair enough - it does! I thought the parser was more strict than that.

Ha...thanks for checking again. i was tinkering with the JSON, and found it to work when doing the exercises.