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

Ramrakesh Azhagesan
Ramrakesh Azhagesan
7,238 Points

Data Parsing final Step

Hello

I am getting the error ""bummer! make sure you write each book to the log exactly as specified in the instructions"

Please advice

i need the format : book title, a comma, a space, and the number of pages.

example : 'book title 1, 300'.

my code output

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

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Can you paste in the rest of your code?

Ramrakesh Azhagesan
Ramrakesh Azhagesan
7,238 Points
//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);
  String title = jsonData.getString("title");
  int pages = jsonData.getInt("pages");
  Log.i("CodeChallenge",title + ", " + pages); 
}

1 Answer

String title = jsonData.getString("title");

int pages = jsonData.getInt("pages");

I had an issue with working through this example for a time as well. This looks exactly like my answer except for: Instead of passing it the jsonData.get line I used the JSONObject that we both createad inside the for loop

So... String title = jsonBook.getString("title");

Instead of: int pages = jsonData.getInt("pages");

I passed it as a string variable. So I changed you're line into:

String pages = jsonBook.getString("pages"); and it worked for me.

However, now seeing that the data.json page does not use double quotes your answer may be more correct.

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Good find! Yes, you are calling getString() and getInt() on jsonData when you should be calling those on jsonBook.