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 trialAdlight Sibanda
5,701 Pointsjson Log.i('CodeChallenge")
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.
// 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);
String title = show.getString("title");
Log.i("CodeChallenge", title);
}
{
"name":"Netflix Playlist",
"shows":[
{
"title":"Doctor Who",
"season":8,
"episode":3
},
{
"title":"Arrow",
"season":1,
"episode":10
},
{
"title":"Portlandia",
"season":4,
"episode":5
}
]
}
3 Answers
Nicolas Hampton
44,638 PointsOMG, I stared at this perfect code for a long time. When you call the JSONArray shows, you accidentally capitalized the s. Fix that, you're golden.
Nic
jenyufu
3,311 PointsHere, I fixed your code and explained it below.
JSONArray jsonShows = jsonData.getJSONArray("shows"); //<-- the reason why yours didn't work was because you put ("Shows") instead of ("shows") here.
for (int i = 0; i < jsonShows.length(); i++)
{
JSONObject jsonShow = jsonShows.getJSONObject(i);
String title = jsonShow.getString("title");
Log.i("CodeChallenge", title);
}
Adlight Sibanda
5,701 Pointshi Nic
Thank you for taking your time to help me with the challenge, unfortunately i made the changes but they are passing not. It`s reporting a syntax error.
for (int i = 0; i < jsonshows.length(); i++)
{
JSONObject show = jsonShows.getJSONObject(i);
String title = show.getString("title");
Log.i("CodeChallenge", title);
}
S.O.S
thanx
Adlight Sibanda
5,701 Pointsooh Nic i finally got it thanx a tonne!!
Nicolas Hampton
44,638 PointsAwesome, glad you got it!
Bhekimpilo Ncube
4,157 PointsHow did you do it Adlight, Im stuck here for ages..
Bhekimpilo Ncube
4,157 PointsNic?, any one???
jenyufu
3,311 Pointscheck this here: https://teamtreehouse.com/forum/what-is-wrong-with-my-codes-i-have-been-stuck-for-a-long-time-on-challenge-2-of-2
dun't know why it works, it seems very different from the other answers but oh well.