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

What is wrong with my codes, i have been stuck for a long time on challenge 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

CodeChallenge.java
JSONArray jsonShows = jsonData.getJSONArray("shows");
for (int i = 0; i < jsonShows.length(); i++){
  JSONObject jsonShows = data.getJSONObject(i);
  show.setTittle(getJSONObject("title"));
  show.setEpisode(getJSONObject("episode"));
  show.setSeason(getJSONObject("season"));
  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
        }
    ]
}

pliz help me to clean up my codes, this is a real challenge and its been days now

3 Answers

jenyufu
jenyufu
3,311 Points

Here is a working code that I did:

JSONArray jsonShows = jsonData.getJSONArray("shows");
for (int i = 0; i < jsonShows.length(); i++)
{
  JSONObject jsonShow = jsonShows.getJSONObject(i);
    String title = jsonShow.getString("title");
    Log.i("CodeChallenge", title);
}
Jon Baum
Jon Baum
13,863 Points

I just tried this to check my answer what I put in and passed with was

JSONArray jsonShows = jsonData.getJSONArray("shows");
for (int i = 0; i < jsonShows.length(); i++) {
  JSONObject jsonShow = jsonShows.getJSONObject(i);
  Log.i("CodeChallenge", jsonShow.getString("title"));
}

There are a few things in your code that should be changed:

  1. You shadow the jsonShows variable, filling it with a show which is a JSONObject rather then the JSONArray (which jsonShows is) which won't work since the the types of the objects are different. Check out variable shadowing for more info on whats going on underneath the hood.

  2. You include a number of different lines filling in the title etc with artificial methods (setTitle, setEpisode, setSeason) these are unnecessary for the code challenge and will simply confuse the program trying to check whether you completed the challenge or not. They write simple tests and if they don't include code with methods for setTitle, setEpisode, setSeason then the test will crash trying to find those methods.

  3. You try logging a nonexistent variable title, you need to instantiate it and fill it with the title of the show.

  4. We have the same problem with the variable data which is never instantiated.

So using your code directly we get

JSONArray jsonShows = jsonData.getJSONArray("shows");
for (int i = 0; i < jsonShows.length(); i++){
       \\Use jsonShow instead of jsonShows here
        \\ JSONObject jsonShows = data.getJSONObject(i);
  JSONObject jsonShow = jsonShows.getJSONObject(i);

     \\Remove the following 3 lines 
      \\show.setTittle(getJSONObject("title"));
      \\ show.setEpisode(getJSONObject("episode"));
       \\show.setSeason(getJSONObject("season"));

        \\Instantiate the variable title and fill it with a string
   String title = jsonShow.getString("title");  
   Log.i("CodeChallenge", title);
}

Then the code should (in principle) work. Hope this helps.

ooohhhh ok thank a lot, it worked out