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

Parsing Data Returned in JSON Format Code Challenge

This is the code challenge: Finally, write a 'for' loop that loops through 'jsonBooks'. In each step of the loop, use 'getJSONObject(int index)' to get a JSONObject from the array. Then use the 'Log.i()' method (with "CodeChallenge" as the tag) to write the book title, a comma, a space, and the number of pages. Ex. output: 'Book title 1, 300'.

I know my code is correct. I've tried multiple syntax to get my code be EXACTLY like the log request they want. Here is my code. It works for sure.

// 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 k=0; k<jsonBooks.length(); k++)
{
  JSONObject jsonBook = jsonBooks.getJSONObject(k);
  String title = jsonBook.getString("title");
    String pages = jsonBook.getString("pages");
    Log.i("CodeChallenge", title + "," + " " + pages);
}

They will not give me credit for this and I am ocd so I want credit for this.

1 Answer

You have one very big problem. You are trying to getString from pages. Pages is an int and not a string.

Secondly, I'm not sure they want you to create a title and pages variable. So we can try to do that all in the log.

Third, (mostly cosmetic) but you can combine the "," + " " into one as so ", ".

This gives us a result of:

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

Again, your major mistake was trying to getString from pages. If you created the two extra variables, then your code should compile anyway. Hope this helps.

Thanks man. That was my problem. I'm surprised it wasn't a compile time error. It kept on giving me a runtime error stating that the log statement wasn't exactly what they wanted. Anyways, I'm done with that.

Ben Jakuben
Ben Jakuben
Treehouse Teacher

This is a common error! In the future search the forum here if you're stuck to see if your question has been asked and answered before.

The biggest problem with our Java/Android code challenge engine is that we can't capture compile-time errors and spit them back to the user. I would like to look into that myself if I can find some time, but until then we have to do our best with providing hints in the "Compilation Error" messages. Sorry you had trouble - you're exactly right - this would have been highlighted differently in a real IDE.

This code challenge was a good two hour lesson. It really made me think about every letter and punctuation mark I put into this code!