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 trialdrizzy drake
Courses Plus Student 2,469 PointsI can't figure out why This isn't working. Please help
i've been looking at it for more than an hour but still can't see my mistake
// 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");
//JSONObject jsonBooks = new JSONObject("books");
for(int i = 0; i < jsonBooks.length(); i++){
JSONObject jsonBook = jsonBooks.getJSONObject(i); //i tried int index
String title = jsonBooks.getString("title");
String pages = jsonBooks.getString("pages");
Log.i(CodeChallenge, " "+title +" "+i+" "+ 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
}
]
}
1 Answer
Ken Alger
Treehouse TeacherDrizzy:
A couple of things...
First, your creating a jsonBook
object, and referencing jsonBooks
in your attempts to get strings. That "s" on the end is creating an issue.
Second, in your line of code:
String pages = jsonBooks.getString("pages");
The data for pages
is not a string it is an int, so we need to change that to:
int pages = jsonBooks.getInt("pages");
Thirdly, the formatting for your output to the log is not what the challenge engine is wanting. The instructions are calling for it to be formatted: 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'.
That would look similar to:
Log.i("CodeChallenge", title + ", " + pages);
Hope that helps a bit, post back if it does not.
Happy coding,
Ken