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
Jaime Betancourt
1,927 PointsI need help with a quiz question.
I have been working on a task three of one of the quizzes and i have not been able to answer it. here is what it is asking.
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'.
and here is what i have.
// 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 i=0; i < jsonBooks.length(); i++){ JSONObject books = jsonBooks.getJSONObject(i); string title = books.getString("title"); String page = books.getString("pages"); Log.i("CodeChallenge", title + ", " + page); }
2 Answers
Jaime Betancourt
1,927 Pointsi have been working on it and i have even checked the video a few times but i still dont get it right. i rewrote the code.
// 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 index = 0; index < jsonBooks.length(); index++){ JSONObject jsonPost = jsonBooks.getJSONObject(index); String title = jsonPost.getString("title"); String page = jsonPost.getString("pages"); Log.i("CodeChallenge", title + ", " + page);
}
Now i get a different error message
Bummer! Make sure you write each book to the log exactly as specified in the instructions.
Silpa Nanduri
647 PointsFound the answer in one of the posts. Here it is https://teamtreehouse.com/forum/i-cant-get-through-the-last-challenge-of-parsing-data-returned-in-json-format
Jaime Betancourt
1,927 Pointsthank you so much.
Silpa Nanduri
647 PointsOops that didn't work either.
Silpa Nanduri
647 PointsI get this error now.
Bummer! Make sure you write each book to the log exactly as specified in the instructions.
This is my code:
for(int i=0; i< jsonBooks.length(); i++){ JSONObject book = jsonBooks.getJSONObject(i); Log.i("CodeChallenge",book.getString("title")+", "+book.getString("pages")); }
Ben Jakuben
Treehouse TeacherHi Silpa,
Check out the discussion in that linked post and it should highlight the error in your code. :)
Jaime Betancourt
1,927 Pointsthat is weird because i just took the quiz again and used your code but it worked for me. maybe you should just go ahead and copy the code from the forum you found.
Silpa Nanduri
647 PointsSilpa Nanduri
647 PointsI get the same error too.