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 (2015) Updating the Data Model Using JSONArray

json Log.i('CodeChallenge")

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.

CodeChallenge.java
// JSONObject 'jsonData' was loaded from data.json
JSONArray jsonShows = jsonData.getJSONArray("Shows");

for (int i = 0; i < jsonShows.length(); i++) 
    {
                    JSONObject show = jsonShows.getJSONObject(i);
                    String title = show.getString("title");
                    Log.i("CodeChallenge", title);                           
    }
data.json
{
    "name":"Netflix Playlist",
    "shows":[
        {
            "title":"Doctor Who",
            "season":8,
            "episode":3
        },
        {
            "title":"Arrow",
            "season":1,
            "episode":10
        },
        {
            "title":"Portlandia",
            "season":4,
            "episode":5
        }
    ]
}

3 Answers

OMG, I stared at this perfect code for a long time. When you call the JSONArray shows, you accidentally capitalized the s. Fix that, you're golden.

Nic

jenyufu
jenyufu
3,311 Points

Here, I fixed your code and explained it below.

JSONArray jsonShows = jsonData.getJSONArray("shows"); //<-- the reason why yours didn't work was because you put ("Shows") instead of ("shows") here.
for (int i = 0; i < jsonShows.length(); i++)
{
  JSONObject jsonShow = jsonShows.getJSONObject(i);
    String title = jsonShow.getString("title");
    Log.i("CodeChallenge", title);
}

hi Nic

Thank you for taking your time to help me with the challenge, unfortunately i made the changes but they are passing not. It`s reporting a syntax error.

for (int i = 0; i < jsonshows.length(); i++) { JSONObject show = jsonShows.getJSONObject(i); String title = show.getString("title"); Log.i("CodeChallenge", title);
}

S.O.S

thanx

ooh Nic i finally got it thanx a tonne!!

Awesome, glad you got it!

How did you do it Adlight, Im stuck here for ages..

Nic?, any one???

jenyufu
jenyufu
3,311 Points

check this here: https://teamtreehouse.com/forum/what-is-wrong-with-my-codes-i-have-been-stuck-for-a-long-time-on-challenge-2-of-2

dun't know why it works, it seems very different from the other answers but oh well.