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


Help with a code challenge? Parsing data returned in JSON format.

I have worked on this code challenge for awhile and I have been fixing all the errors I was making, but I'm not sure how to fix this one.

The error states: Bummer! Your loop should run twice and call 'getJSONObject()' each time through.

I'm not sure if I'm supposed to be changing the jsonBooks.length and if so what do I change it to?

`String name= jsonData.getString("name"); String publisher= jsonData.getString("publisher"); String language= jsonData.getString("language"); JSONArray jsonBooks= jsonData.getJSONArray("jsonBooks");

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

1 Answer

Joseph Kato
Joseph Kato
35,340 Points

Hi Ginger,

It looks like you have two minor issues.

First, this line:

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

Should be:

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

And this:

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

Should have a space after the comma, making it:

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

Thank you!