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
Naveen Selvasekaran
825 PointsQuestion on JSON code challenge
This is my solution. I am not able to find out the problem. The error says "Bummer! Make sure you write each book to the log exactly as specified in the instructions." Any help?
String name = jsonData.getString("name");
String publisher = jsonData.getString("publisher");
String language = jsonData.getString("language");
JSONArray jsonBooks = jsonData.getJSONArray("books");
for(int index=0;index<jsonBooks.length();index++){
JSONObject jsonObject = jsonBooks.getJSONObject(index);
Log.i("CodeChallenge","book title"+jsonObject.getString("title")+", "+jsonObject.getInt("pages"));
}
4 Answers
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"));
}
So it seems that you just included an unnecessary:
"book title"+
Just take that out, and you are good to go. Keep coding!
James Barnett
39,199 PointsIt help to have a link to which code challenge you are stuck on
Naveen Selvasekaran
825 PointsYeah, thank you. I just added the text to give a label.
yuehwencheng
29,154 PointsHi, I'm on the same challenge and somehow can't get it done yet. Here're my codes which seem to be the same as the above answer:
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);
String title = jsonBook.getString("title");
String author = jsonBook.getString("author");
String pages = jsonBook.getInt( "pages");
}catch(JSONException e){
System.out.println("Exception: "+e);
}
//output.html
JavaTester.java:76: cannot find symbol
symbol : variable name
location: class JavaTester
else if (!name.equals(jsonData.getString("name"))) {
^
JavaTester.java:79: cannot find symbol
symbol : variable publisher
location: class JavaTester
else if (!publisher.equals(jsonData.getString("publisher"))) {
^
JavaTester.java:82: cannot find symbol
symbol : variable language
location: class JavaTester
else if (!language.equals(jsonData.getString("language"))) {
^
3 errors