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

Android

Krzysztof Paz
Krzysztof Paz
7,361 Points

CodeChallenge in Build a Blog Reader Android App - Getting Data from the Web - Parsing Data Returned in JSON Format

Hi, I'm in 3rd step of Build a Blog Reader Android App - Getting Data from the Web - Parsing Data Returned in JSON Format - codeChallenge 3/3 task of: "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 I have no clue what is wrong with my code, that makes it not passing as expected, but just giving the message: "Bummer! Make sure you write each book to the log exactly as specified in the instructions."

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

Could you please advise, how to make it right?

I am having the exact same problem. I keep getting the error message "Make sure you write each book to the log exactly as specified in the instructions."

my code:

for (int i = 0; i < jsonBooks.length(); i++){ JSONObject jsonBook = jsonBooks.getJSONObject(i); String title = jsonBook.getString("title"); String pages = jsonBook.getString("pages"); Log.i("CodeChallenge", title + ", " + pages);

Krzysztof Paz
Krzysztof Paz
7,361 Points

Ok, I've found the Ben's trap ;-)

Just change the pages variable to Integer and use the getInt for it as well, then it goes like it should.

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

Regards, Kris.

Thanks!

Of course, getInt in order to get an integer! I was not seeing the answer. I had set the variable as int but kept using getString. I can now go to sleep ready to try the next steps of the code.

Cheers,

Randell

Dan Giles
Dan Giles
2,243 Points

Good catch Krzysztof; thanks! I was banging my head against my keyboard wondering why this wasn't working.

Note that leaving it as 'book.getString("pages")' should also work if used in actual code, since the JSONObject will coerce the int into a String for this method return. From http://developer.android.com/reference/org/json/JSONObject.html:

"When the requested type is a String, other non-null values will be coerced using valueOf(Object)"

2 Answers

Nick Pettit
STAFF
Nick Pettit
Treehouse Teacher

Looks like this post is figured out! :) I'm just answering in the answer box so that this post doesn't appear in the "unanswered" section of the forum anymore.

When answering posts, be sure to reply in "Answer" section so that you can get points for it!

Thnaks