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

Tremayne Clinton
Tremayne Clinton
5,417 Points

Need some help pls

Trying to write a for loop what am I doing wrong

CodeChallenge.java
JSONArray jsonShows = jsonData.getJSONArray("jsonShows");

for (int i = 0; i < data.length(); i++){
     JSONObject = data.getJSONObject(i);
     Show show = new Show();
     show.setTitle(jsonShows.getJSONObject("title"));
     show.setSeason(jsonShows.getJSONObject("season"));
     show.setEpisode(jsonShows.getJSONObject("title"));
       Log.i(TAG, "CodeChallenge:");
}








// JSONObject 'jsonData' was loaded from data.jso
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
        }
    ]
}

1 Answer

Jon Kussmann
PLUS
Jon Kussmann
Courses Plus Student 7,254 Points

In the first line of your for loop, you have not created a name for the variable to hold the JSON object.

I think you meant to have: JSONObject jsonShows= data.getJSONObject(i);

Jon Kussmann
Jon Kussmann
Courses Plus Student 7,254 Points

Sorry, I had to go back and actually look at the question you were referring to.

JSONArray jsonShows = jsonData.getJSONArray("shows");

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

}

jsonShows is the array that you want to loop through... so that is the object you want to check the length of to define the for loop. You are creating an object of the Show class (or at least are trying to), but you don't have one defined (not that you need it for this question).

The "CodeChallenge" is the TAG that you want to use, and for the second parameter of the Log.i method, you want the title of the show.

Does that make sense?

Tremayne Clinton
Tremayne Clinton
5,417 Points

I'll watch the video 5 or 6 more times to see if I can see what he wants me to do

Jon Kussmann
Jon Kussmann
Courses Plus Student 7,254 Points

Is there a section of the code I posted here that does not make sense?

Tremayne Clinton
Tremayne Clinton
5,417 Points

just leaning less than a week so it all new to me I follow the example just mean I need more work
on understanding what I'm to do