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

Help Debugging this code ,error in Log.i();

Hi! i keep getting an error and i believe it to be in the log.i() line. Any assistance will be highly appreciated. // JSONObject 'jsonData' was loaded from data.jsonJSONArray jsonBooks = jsonData.getJSONArray("books"); 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 jsonBook = jsonBooks.getJSONObject(i); int pages = jsonBook.getInt("pages"); Log.i("CodeChallenge", "title" + ", " + pages); }

// JSONObject 'jsonData' was loaded from data.json

2 Answers

Daniel Hartin
Daniel Hartin
18,106 Points

The problem you are having is that you are using the string title not the book title. You need to change the last line of your for loop where it creates the string in the Log to the code below:

(basically reference the book title by creating a new string variable and assigning it the book title and use that)

```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 jsonBook = jsonBooks.getJSONObject(i); int pages = jsonBook.getInt("pages"); String title = jsonBook.getString("title"); Log.i("CodeChallenge", title + ", " + pages); }```

Hope this helps Daniel

Thank you very much Daniel, this has helped me alot. Beautiful family you have there by the way.

Daniel Hartin
Daniel Hartin
18,106 Points

Thanks, glad I could help!

Ty Daniel Hartin, this helped me.