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 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