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

Rafael Moreno
Rafael Moreno
16,622 Points

Getting Data From website coding challenge

Hi there, I was stuck on this question on the coding challenge. Any help as to why its not working for me will be greatly appreciated. Thanks!

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

String name = jsonData.getString("name");
    String publisher = jsonData.getString("publisher");
    String language = jsonData.getString("language");

JSONArray jsonBooks = jsonData.getJSONArray("books");

for(int i = 0; jsonBooks.length(); i++){
 JSONObject jsonBook = jsonbooks.getJSONObject(i);
    Log.i(CodeChallenge, i + title + ", " + pages);
}
Kevin Jennings
Kevin Jennings
3,630 Points

Just FYI: Use the key (the one right by the number one key, with the tilde (~) on it) to format your code. I did the same thing you did and used the apostrophe at first. When I saw that it didn't format my code, I tried the key, at it worked. I think it's called a "backward tick mark" or something like that.

6 Answers

Rafael Moreno
Rafael Moreno
16,622 Points

it does not start with Java String name. Its supposed to be Java (enter) String name =

Kevin Jennings
Kevin Jennings
3,630 Points

Your for loop looks like it might be missing something in the terminator section. I may be wrong, but shouldn't you compare the length of jsonBooks to i so that the loop knows when to end?

Like: for(int i = 0; i < jsonBooks.length(); i++)

Rafael Moreno
Rafael Moreno
16,622 Points

ah. I think that is one of the problems but it has not accepted my answer yet, even after I fix it.

Kevin Jennings
Kevin Jennings
3,630 Points

I'm not familiar with Java, but the Log() method seems to take a String as it's first parameter. So, in your for loop, you may need to add quotes around the word CodeChallenge. Also, where is title coming from? I don't see that variable defined in your code. If it's coming from the jsonBook object, then I think you have to access it this way: jsonBook.title.

So, the section in the for loop that calls the Log() method should probably look something like this:

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

Again, I'm not familiar with Java or the lesson you are attempting, so my answer may not be spot on.

Rafael Moreno
Rafael Moreno
16,622 Points

There is JSON code in a separate file where we are taking in the values for title and pages.

{
    "name":"Treehouse Book Series",
    "publisher":"Wiley",
    "language":"English",
    "books":[
        {
            "title":"HTML5 Foundations",
            "author":"Matt West",
            "pages":384
        },
        {
            "title":"CSS3 Foundations",
            "author":"Ian Lunn",
            "pages":352
        }
    ]
}
Rafael Moreno
Rafael Moreno
16,622 Points

Thanks for the markdown tip!

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

It doesn't look like you are declaring or setting the title and pages variables anywhere. You also won't need to use the i variable in the Log statement.