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

Android

Android Lists and Adapters, Updating the Data Model section, Using JSONArray challenge

Link to challenge: http://teamtreehouse.com/library/android-lists-and-adapters/updating-the-data-model/using-jsonarray

Challenge Task 1 of 2

The data in the file data.json has been loaded into a JSONObject named jsonData. Using data.json as your guide, create a JSONArray object named jsonShows and set it to the array of shows from data.json using the jsonData object.

This challenge took me hours and hours of research. The videos were no help (since the code in them is not Google searchable). No code downloads for this course so you are on your own.

I searched far and wide for any code that used "jsonShows" but there was none.

Being a little more flexible in my searching I came across this forum post:

https://teamtreehouse.com/forum/codechallenge-in-build-a-blog-reader-android-app-getting-data-from-the-web-parsing-data-returned-in-json-format

..which had this line:

JSONArray jsonBooks = jsonData.getJSONArray("books");

It's pretty close!

So we are dealing with "shows" instead of "books"

(do that substitution first) ,

and if you substitute 'jsonShows' for 'jsonBooks' ..

You end up with code that gets you through the first challenge.


Then came the really hard part of the challenge:

Challenge Task 2 of 2

Finally, write a 'for' loop that loops through jsonShows. In each step of the loop, use getJSONObject(int index) to get a JSONObject from the array. Then use the Log.i() method (with "CodeChallenge" as the tag) to write the show title.

"Oh no, please don't ask me to write a for loop" (I whine).

Head is hurting as I have to try to remember what a Java for loop looks like...

Wait..there is probably something in that same forum thread:

for (int i = 0; i < jsonBooks.length(); i++) 
    {
                    JSONObject book = jsonBooks.getJSONObject(i);
                    String title = book.getString("title");
                    String pages = book.getString("pages");
                    Log.i("CodeChallenge", title+", "+pages);                           
    }

..well at least there is something about

Log.i("CodeChallenge",

Hmmm... Maybe if I change out 'jsonBooks' (wherever it appears) with 'jsonShows'..

Good thought, but it's still giving me an error!

Doesn't seem to like 'book'

Okay, we'll change that wherever it says 'book' to use 'show'.

And we just need title so get rid of the line with "pages" which means means simplifying the line below it to:

Log.i("CodeChallenge", title); 

..and I finally got something that passed.


Hopefully this helps someone (without directly giving the code for the answer).

Tyler Cassity
Tyler Cassity
4,873 Points

Ive been stuck on this challenge for the last couple days! Thank you for sharing your findings, and thank you for the breakdown of your thought process! :)

jenyufu
jenyufu
3,311 Points

thanks, that was helpful

Brilliant, this helped me a lot. Thank you for posting this.

3 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Mr. White;

I thought Mr. Jakuben walked through a very similar piece of code in the Introducing JSONArray and From JSONArray to Java Array videos quite effectively. Did you not think they were covered well enough to complete the challenge tasks?

To the best of my recollection, all of the program files are available in the Downloads section beneath each video, or like I mentioned, if you work along with Mr. Jakuben in a "shadow" project on your own system the code would be readily accessible.

Congratulations on sticking with the challenge and working through it though. I find it rewarding when delving into challenges that stretch my knowledge and making it out the other side.

Happy coding,

Ken

Ben Jakuben
Ben Jakuben
Treehouse Teacher

I'm really glad to see discussions like this. I want these challenges to be representative of real tasks and at least somewhat challenging without causing too much frustration. I think this one is pretty reasonable as is but will keep an eye on the feedback to see if I need to update it somehow.

I think the challenge is quality. I found it a little tricky as it pulled in past concepts of adding the String variable in the for loop. The problem I had was on my mental side... for some reason I wanted to add in the season and episode data. So maybe this would be a good 3rd challenge to add? It probably would have cleared my log.i error I was getting... LOL

jenyufu
jenyufu
3,311 Points

nah, the videos in this series was hard to follow. It didn't explain why we were doing what we were doing enough. I felt like I was just copying what was on screen and not knowing the larger concept/idea behind it.

John Keith Zank
John Keith Zank
11,848 Points

I think Ben did a great job in the videos discussing what each section did but, if I can add just a tiny bit of constructive criticism, perhaps go a bit slower and add comments throughout the code. I've been playing around with Java for about a year now as a CS student and it took me a while to really understand everything going on, even after re-watching certain parts of the videos.

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Thanks for the feedback, John, and duly noted! We take feedback like this into consideration when refreshing or improving existing courses, so it's good to hear.

Tobias Mahnert
Tobias Mahnert
89,414 Points

I disagree that the treehouse challenges are representative for real task challenges. You have to use a predefined way but to reach a programming goal you can use many different ways. The treehouse Code Challenges are very strict. One mistake in the predefined syntax and you don't know whats going on, even you have the result you wanted in the editor. Its very important therefor that treehouse provides us with the way they want so that we can progress. To describe a case in the video but ask us for a different topic in the challenge is a frustrating experience and NOT helpful at all. Remember that you probably dealing with very very beginners. Imagine you have a baby and you show them how to bind your shoes but expect them to do it with one hand when its their turn. Just saying... peace...

Tobias Mahnert
Tobias Mahnert
89,414 Points

Tendai, you don't get my point. There are different ways of making something hard. You can give a problem and its your problem to find the solution but the way how you get the solution can be your own choice. That's productive and helpful learning. Or you have a problem and must find the way the problem teller wants from you. Thats contra productive learning because it doesn't support your creativity. You basically learn what your prof wants from you and not what's possible. Treehouse often gives you a code challenge where you have to find the way the teller wants from you, not (always) whats all possible to do it. To say treehouse codechallenges are related to realworld cases is fucking bullshit because its not. The realworld gets and provides creative ways and autocomplete code editors. The benefit of mastering treehouse challenges is to provide you with some kind of repertoire which will help you to deal with real world problems on your own, and therefore its neccesary that we get codechallenges which gives us problems that are explained in detail in the videos and not, how it is the case in this challenge, a challenge which is different then to what it teaches us in the video. peace