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 trialRafael Moreno
17,345 PointsGetting 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);
}
6 Answers
Rafael Moreno
17,345 Pointsit does not start with Java String name. Its supposed to be Java (enter) String name =
Kevin Jennings
3,630 PointsYour 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
17,345 Pointsah. I think that is one of the problems but it has not accepted my answer yet, even after I fix it.
Kevin Jennings
3,630 PointsI'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
17,345 PointsThere 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
17,345 PointsThanks for the markdown tip!
Ben Jakuben
Treehouse TeacherIt 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.
Kevin Jennings
3,630 PointsKevin Jennings
3,630 PointsJust 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.