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
Michael Quiapos
Courses Plus Student 14,537 PointsI can't get through the last challenge of "Parsing Data Returned in JSON Format"
No matter what I do, the challenge result is always "Compilation Error! Verify your syntax, especially in the 'for' loop. Remember that the 3 statements in the parenthesis of the 'for' loop are separated by semicolons."
Can anyone please let me know what am I doing wrong?
// 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 jsonBook = jsonBooks.getJSONObject(i); Log.i("CodeChallenge","title"+", "+"pages") }
1 Answer
Ernest Grzybowski
Treehouse Project ReviewerI believe you were at:
Build a Blog Reader Android App > Getting Data from the Web > Parsing Data Returned in JSON Format
Step one:
String name = jsonData.getString("name");
String publisher = jsonData.getString("publisher");
String language = jsonData.getString("language");
Step two:
JSONArray jsonBooks = jsonData.getJSONArray("books");
Step three:
for(int index=0;index<jsonBooks.length();index++){
JSONObject jsonObject = jsonBooks.getJSONObject(index);
Log.i("CodeChallenge",jsonObject.getString("title")+", "+jsonObject.getInt("pages"));
}
Keep coding!
Kunal Patel
1,448 PointsKunal Patel
1,448 PointsThis lesson is actually flawed for 2 reasons.
It's ambiguous, how I write the Log.i could be via a String or Int for pages. The challenge doesn't specify.
How it's written can be left up to interpretation with several valid instances, all of which fail the analyzer. Is Valid yet fails:
And finally your solution can be changed to:
Log.i("CodeChallenge",jsonObject.getString("title")+", "+jsonObject.getString("pages"));Kunal Patel
1,448 PointsKunal Patel
1,448 PointsForgive my formatting, it didn't seem to take here.
Ben Jakuben
Treehouse TeacherBen Jakuben
Treehouse TeacherHi Kunal,
There are actually errors in each of your examples, which is why they fail. The first has a capital "I" for int, and the second declares 'pages' as a String variable. See this other Forum post for more info on that. Your last line has a similar problem about how you are getting "pages" from the JSONObject.
This code challenge has been a little frustrating for students, but it is a very valid example of real life programming headaches. :)
Kunal Patel
1,448 PointsKunal Patel
1,448 PointsAh yes, sorry it should be int pages not Int pages. I should've caught that.
Kunal Patel
1,448 PointsKunal Patel
1,448 PointsHmm, why is String pages = book.getString("pages"); invalid? Can't we extract a string for the pages?
Ben Jakuben
Treehouse TeacherBen Jakuben
Treehouse TeacherIt's tough because our Code Challenge engine doesn't give you compile-time errors like a real IDE. Hopefully that's something we can address in the future.
"pages" is actually a number value in the data.json file. As such we can't use the
getString()method to parse it. It won't implicitly convert things like that for us; we need to use the specific parsing method (getInt()in this case).Kunal Patel
1,448 PointsKunal Patel
1,448 PointsInteresting, this has definitely gotten me a little confused.
When I look at the url: http://blog.teamtreehouse.com/api/get_recent_summary/?count
I see pages as, "pages":148 where 148 is not in quotes. The below however works, not only compile time, but also saves the parsed object to String pages during run time.
This is not supposed to compile?
Ben Jakuben
Treehouse TeacherBen Jakuben
Treehouse TeacherFair enough - it does! I thought the parser was more strict than that.
Kunal Patel
1,448 PointsKunal Patel
1,448 PointsHa...thanks for checking again. i was tinkering with the JSON, and found it to work when doing the exercises.