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 Build a Blog Reader Android App Getting Data from the Web Parsing Data Returned in JSON Format

Error saying I am not following the exact model given

So in this code challenge, I am getting an error saying that I am not following the exact example given for the Logging model, but I have spent about an hour just reviewing and I see nothing wrong. Here is the log statement:

String title = jsonData.getString("title"); String pages = jsonData.getString("pages"); Log.i("CodeChallenge", title + ", " + pages);

Please help! I have no idea why it says I am not following the model of the book title, a comma, a space, and then the pages! I have even tried:

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

And that still didn't work!

1 Answer

ok your problem is your trying to access the title and pages from root of JSON. You need to get array of books and then loop through them to get each title and page.

// get the array of books
JSONArray  jsonBooks = jsonData.getJSONArray("books");

// then iterator of   jsonBooks array with for loop.

for(int i = 0; i < jsonBooks.length(); i++) {                             // loops through all the books in the jsonBooks array
  JSONObject jsonBook = jsonBooks.getJSONObject(i);   // get  single book with index of i
  String title = jsonBook.getString("title");                          // store the title of the book into title string variable
  String pages = jsonBook.getString("pages");                  // store the total pages in the book into  pages string variable
  Log.i("CodeChallenge", title + ", " + pages); 
} 

hope This helps