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

i need help with JSONArray

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 <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
        }
    ]
}

2 Answers

jenyufu
jenyufu
3,311 Points

Here I fixed it for you, here is what went wrong: your for(conditional) line is wrong. Just two slight mistakes. Its suppose to look like this:

for (int i = 0; i < jsonShows.length(); i++) 

yours was this:

for (int i = 0 <jsonShows.length(); i++);    //<-- you forgot the 'i'. And don't put the ";" at the end of the conditional.

Here is the entire snippet fixed, I tested it in the challenge and it completed:

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);
}

I would love to help you, but please use the search before you post a new thread. This challenge has been explain numerous type by different users. Here are some examples:

https://teamtreehouse.com/forum/what-am-i-doing-wrong-109

https://teamtreehouse.com/forum/code-challenge-engine-cant-find-tag

https://teamtreehouse.com/forum/codechallenge-javalangnullpointerexception