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

Jeremiah Chersin
Jeremiah Chersin
3,601 Points

Having trouble with, parsing data returned in JSON format objective three.

The objective question follows below.

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

Not quite sure how I am supposed to do this part. Any help is greatly appreciated.

2 Answers

Matheus G Oliveira
Matheus G Oliveira
9,682 Points

Hey Jeremiah,

For the next question, remember posting the code you are having issues so we can help you based on your mistakes.

For now, to solve your code, the answer should be something like this

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

Thanks! Alright, I'll be sure to do that next time, thanks for the input!

Matheus G Oliveira
Matheus G Oliveira
9,682 Points

Cool, add it as the best answer then

Good luck!