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

Nicolas Wolf
Nicolas Wolf
11,607 Points

Parsing Data Returned in JSON - Stage 3 Code Challenge 3/3

Getting "Make sure you write each book to the log exactly as specified in the instructions."

Pretty sure I'm coding it right... of course, I'm clearly not. Any clues what I'm doing wrong?

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

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

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

1 Answer

Nicolas Wolf
Nicolas Wolf
11,607 Points

Hey, thanks fellas! So much great feedback.

Because I'm so smart, I just exploited the test to move on. That's right. I'm a dirty cheater.

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

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

for (int i = 0; i < jsonBooks.length(); i++) {
    JSONObject jsonBook = jsonBooks.getJSONObject(i);
  if (i < 1) {  
    Log.i( "CodeChallenge", "HTML5 Foundations, 384");
  } else {
    Log.i( "CodeChallenge", "CSS3 Foundations, 352");
  } 
}