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
brian colliflower
15,433 Pointsparsing data returned from json format
task three on this code challenge is giving me fits it dosent matter how i modify the code i get an error . anyway here is my code if anyone can help i would be greatful.
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");
int pages = jsonBook.getInt("pages");
Log.i("CodeChallenge", title + ", " + pages);
12 Answers
Hasan Tugkan Kibar
2,230 PointsCan you change the last bit of your code from pages to pages.toString()? As I recall that was the problem.
brian colliflower
15,433 PointsI just tried that it didnt help do i have a stupid syntax error somewhere i missed. I also tried putting the .toString after the Log.i() method. I am at a loss thanks for the help
Hasan Tugkan Kibar
2,230 PointsThat's interesting. Sadly, I am unable to see the challenge again(Why treehouse why?) so I'm trying to recall it. I know this might be an overkill but nevertheless can you change the Log.i code with this and try?
String pagesString = pages.toString();
Log.i("CodeChallenge", title + ", " + pagesString);
Ben Jakuben
Treehouse TeacherYou should be able to view it again, but the navigation may be wonky due to some recent changes. Are you still having trouble finding it?
brian colliflower
15,433 Pointslol nope what I dont get is all of these would work in eclipse I am not sure why code challenge wouldnt take them.
Hasan Tugkan Kibar
2,230 PointsThat is really intriguing. Can you check other parts of your code or put it here as code(use 3 backticks : ` at the beginning and at the end) so that we can check it out?
brian colliflower
15,433 Pointsstep one goes through::: String name = jsonData.getString("name"); String publisher = jsonData.getString("publisher"); String language = jsonData.getString("language"); ::: step 2 does as well ::: JSONArray jsonBooks = jsonData.getJSONArray("books"); ::: step three is the break down ::: for(int i = 0; i < jsonBooks.length(); i++){ JSONObject jsonBook = jsonBooks.getJSONObject(i); String title = jsonBook.getString("title"); int pages = jsonBook.getInt("pages"); Log.i("CodeChallenge", title + ", " + pages.toString ); } ::: i think that is what you were wanting i also tried the other methods you mentioned.and they did not work
Hasan Tugkan Kibar
2,230 PointsMate, after toString method you must add the opening and closing parenthesis. It's not toString rather it's toString()
brian colliflower
15,433 PointsYea I know I screwed that retyping in to the form it was written correctly in the challenge just to be safe i tried it again. still same out come
Hasan Tugkan Kibar
2,230 PointsWell, if that's the case I'm terribly sorry I am of no use. Perhaps our beloved teacher, Ben, can look at the matter on hand?
Ben Jakuben
Treehouse TeacherJust an FYI: you can tag anybody with the @ sign and then typing their name. :) Happy to be tagged and help!
brian colliflower
15,433 PointsWell Thank you for trying I really appreciate the help even if it didnt work out
brian colliflower
15,433 PointsOk so i finaly got it to go through I am not sure why
for (int i = 0; i < jsonBooks.length(); i++) {
JSONObject jsonBook = jsonBooks.getJSONObject(i);
Log.i("CodeChallenge", jsonBook.getString("title")
+ ", " + jsonBook.getInt("pages")); }
works and this doesn't:
for(int i = 0; i < jsonBooks.length(); i++){
JSONObject jsonBook = jsonBooks.getJSONObject(i);
String title = jsonBook.getString("title");
int pages = jsonBook.getInt("pages");
Log.i("CodeChallenge", title + ", " + pages);
}
One completes it all in one movment while the second does it in 2 . the second is less efficent not sure why it doesnt work
Ben Jakuben
Treehouse TeacherI am sorry about your frustration with this. The intent (and the code challenge tests) is to use getInt for the pages parameter, which you have here. However, getString is perfectly acceptable, as you saw in Eclipse. I'm making some code challenge updates with the latest new batch I'm working on and will fix this in the next week or two.
brian colliflower
15,433 PointsI got the code to work I am still not sure why the first code works and the second didnt.
Ben Jakuben
Treehouse TeacherThe code from your original post? It looks okay except for the missing curly brace at the end. It could have been a syntax error or even a temporary problem with the Code Challenge engine. If you have specific code that doesn't work, paste it in a reply and we can talk through it.
brian colliflower
15,433 Points@bendog24 OK I believe I have it figured it out. However I wondered if there was a way to contact you more privately I have a question on an app I am building and I would rather not see it out there before I publish it. I was also I was wondering if there were any resources out there.I have realized the more I learn the less I know. I have taken Java 1 and 2 at college and there is no 3. I have taken both your apps I learned more during your apps sadly. anyway I took an online assessment and I bombed. So I am wondering will there be a third app java based or somewhere to learn what i need to get a job. and professionally what is most important to be good in this line of work.
Ben Jakuben
Treehouse TeacherSorry, I missed this comment from a few weeks ago! These are great questions: you should post a new thread in the Forum to get feedback about these from me and other students.