Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Android Lists and Adapters (2015) Updating the Data Model Using JSONArray

Ricky Sparks
Ricky Sparks
22,249 Points

JSON challenge????

Not sure what the question is asking for?? Did I add to much code or not enough?

CodeChallenge.java
// JSONObject 'jsonData' was loaded from data.json
Show[] shows = new Show[data.length()]; {
            for (int i=0; i< data.length(); i++) {
                JSONArray jsonShow = data.getJSONObject(i);
                Show show = new Show();

                data.setTitle(jsonShows.getString("title"));
                data.setSeason(jsonShows.getString("season"));
                data.setEpisode(jsonShows.getString("episode"));



                shows[i] = show;
        }
            return shows;
    }
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
        }
    ]
}
David Axelrod
David Axelrod
36,073 Points

Hrmmm Im on it! I'll be right back!

2 Answers

David Axelrod
David Axelrod
36,073 Points

Hey! Think of jsonData as one of those giant tool things. Some drawers are small, some are big and some have other smaller drawers in them.

Our toolbox is a bit of a security freak in the sense that it requires you to specify the exact thing you want before you can go and get it

First we must declare a JSONArray and ask our toolbox (politely of course) to put the array of shows into the variable jsonShows.

The drawer we just got is split into 3 parts (3 different shows). I'll walk through what we do to the first show and let the for loop take care of the last 2.

Since a show is an object, we ask the drawer for just the section we want (at position i) and put that into the show variable.

in the log statement, the tag comes first (surrounded by quotes!) and then we ask our magical little section of the drawer to get the string named title!

Boom! We're done!

Here's the code

// 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);
  Log.i("CodeChallenge", show.getString("title"));
}
Viviana Bedoya Castro
Viviana Bedoya Castro
2,991 Points

Hello Ricky and David, i was having trouble with this challenge too and i feel so relieved to see that i was not the only one. Sometimes is hard to know what is the answer that the toolbox wants. But thank you both for taking the time to write and answer this question for all of us.

David Axelrod
David Axelrod
36,073 Points

^_^ of course!!

this community is amazing