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

Ben Mair
Ben Mair
8,728 Points

jsonArray JsonShow

having different errors mainly pertaining to the value of i variable.......any thoughts?

CodeChallenge.java
JSONArray jsonShows = jsonData.getJSONArray("Shows");
for (int i=0;  i<jsonShows.length(); i++); {
  JSONObject showsObject = jsonShows.getJSONObject(i);
  Log.i("CodeChallenge", showsObject.getString("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
        }
    ]
}
Stephen Little
Stephen Little
8,312 Points

I am having the same problem and I don't think our code looks really different.... did you find out what was wrong?

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

for (int i = 0; 1 < jsonShows.length(); i++){
  JSONObject show = jsonShows.getJSONObject(i);

  Log.i("CodeChallenge", show.getString("title"));
}
Stephen Little
Stephen Little
8,312 Points

Try changing your "Shows" to just lower s "shows"

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

to

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

That is what I believe the JSONArray name value is...

Cheers! Stephen

Stephen Little
Stephen Little
8,312 Points

Ok my code does not have any compiler errors so I must be logging wrong somehow. Should I put the "title" into a var before using in the Log call? Not sure if that would make any difference or not,

1 Answer

Stephen Little
Stephen Little
8,312 Points

I think what will solve your problem is just the letter "S" to a small "s" in your array call. I found my problem, I had the number 1 in my for loop instead or the "i" like it should of been (DOH!).... I hope your code work once you get that crazy "S" under control, silly "S" stop being on difficult :)

Cheers! Stephen