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

Stuck on the code challenge; Parsing Data Returned in JSON Format objective 3

i am getting the message; Bummer! Your loop should run twice and call 'getJSONObject()' each time through. I am doing my best but don't get now I am lost, guess need to practice more.

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", jsonBook.getString("title") + ", " + jsonBook.getInt("pages"));
}

4 Answers

Ray Snyder
Ray Snyder
1,238 Points

Just did this one a little while ago. If I remember correctly, in JSONArray jsonBooks = jsonData.getJSONArray("Books"); I believe the name of the array is "books" not "Books", is it not?

John McDonald
John McDonald
13,958 Points

Hey,

I was stuck on this part too.

I took your code, formatted it and changed "Books" to "books" and then it worked!

Hope this helps :)

Hi Ray,

Unfortunately it's not the Books but thank you for your effort.

Wow. That worked for me John. Thanks! I've been on this one for about 30 minutes and couldn't figure out why.

Changing:

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

to:

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

worked. Odd that it accepts the first one for part 2, but not in part 3.