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

General Discussion

parsing 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

Can you change the last bit of your code from pages to pages.toString()? As I recall that was the problem.

I 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

That'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
Ben Jakuben
Treehouse Teacher

You 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?

lol nope what I dont get is all of these would work in eclipse I am not sure why code challenge wouldnt take them.

That 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?

step 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

Mate, after toString method you must add the opening and closing parenthesis. It's not toString rather it's toString()

Yea 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

Well, 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
Ben Jakuben
Treehouse Teacher

Just an FYI: you can tag anybody with the @ sign and then typing their name. :) Happy to be tagged and help!

Well Thank you for trying I really appreciate the help even if it didnt work out

Ok 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
Ben Jakuben
Treehouse Teacher

I 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.

I got the code to work I am still not sure why the first code works and the second didnt.

Ben Jakuben
Ben Jakuben
Treehouse Teacher

The 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.

@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
Ben Jakuben
Treehouse Teacher

Sorry, 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.