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 trialMario Zamora
1,396 PointsWhy Log.i () method doesn't run ?
hi,
I'm about to accomplish the CodeChallenge for the activity 2 of 2, but when i'm required to write a for loop to add JSONObjects into a JSONArray everything seems to be right however i got an error because the TAG is nos declared correctly. I think i create the for loop succesfully but i don't know why is the TAG the problem.
// JSONObject 'jsonData' was loaded from data.json
JSONArray jsonShows = jsonData.getJSONArray("shows");
for(int i=0; i<jsonShows.length(); i++) {
JSONObject test = jsonShows.getJSONObject(i);
}
Log.i( TAG , "CodeChallenge" );
{
"name":"Netflix Playlist",
"shows":[
{
"title":"Doctor Who",
"season":8,
"episode":3
},
{
"title":"Arrow",
"season":1,
"episode":10
},
{
"title":"Portlandia",
"season":4,
"episode":5
}
]
}
4 Answers
Kourosh Raeen
23,733 PointsHi Mario - The challenge says to use "CodeChallenge" as the tag so use that string as the first argument of Log.i()
. The second argument needs to be the show's title. To get the title call the getString()
method on the current JSON object in the loop with the argument "title". Also, the Log.i statement needs to be inside the loop.
Mario Zamora
1,396 Pointshi Kourosh,
Thank you so much for your help. i add the code according to your previous remark and what i understood:
JSONArray jsonShows = jsonData.getJSONArray("jsonshows");
for (int i=0; i<jsonShows.length(); i++) {
JSONObject test = jsonShows.getJSONObject(i);
String title = test.getString("title");
Log.i("CodeChallenge" + title);
}
but it throws an error:
Your loop should run three times and call 'get JSONObject()' each time through
Thanks in advance,
Kourosh Raeen
23,733 PointsThe key for the JSONArray is "shows" not "jsonShows":
JSONArray jsonShows = jsonData.getJSONArray("shows");
Also, Log.i
needs two argument so instead of concatenating title
to the string "CodeChallenge" you need to pass it as the second argument:
Log.i("CodeChallenge", title);
Mario Zamora
1,396 PointsThank you so much Kourosh,
I already got my Bagde for completing the module Updating the Data Model. The right code is:
for(int i=0; i<jsonShows.length(); i++){
JSONObject matrix = jsonShows.getJSONObject(i);
String title = matrix.getString("title");
Log.i("CodeChallenge", title);
}
I really appreciate your help
:)