Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android

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!