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 trialMalcolm Mutambanengwe
4,205 PointsJSON Title
Hi Steve, hope you've been well. I'm trying to print JSON data specifically title. I don't understand what I'm getting wrong. Steve Hunter
// JSONObject 'jsonData' was loaded from data.json
JSONArray jsonShows = jsonData.getJSONArray("shows");
for (int i = 0; i < jsonShows.length(); i++) {
JSONObject jsonData = jsonShows.getJSONObject(i);
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
}
]
}
5 Answers
Steve Hunter
57,712 PointsSorry - I was being dim.
You need to access the JSONObject
with the key "title" to bring back the required output. The getString
method will do that for you. So, create the JSONObject, as you have done. then call getString
on that in your output Log
and pass in the key of 'title'.
That gives you:
JSONArray jsonShows = jsonData.getJSONArray("shows");
for (int i = 0; i < jsonShows.length(); i++) {
JSONObject title = jsonShows.getJSONObject(i);
Log.i("CodeChallenge", title.getString("title"));
}
Steve.
Malcolm Mutambanengwe
4,205 PointsHi Steve
The mind never swithces off hey. You were being dim? I am totally useless, I can't even be a bad example. LoL
I think I get what you're saying. The Log.i() method takes a string and an int as input. So I can't use "title" as it is because its a string not an int. As i is incrementing from 0 to 2, it takes the value of the array element from "title" starting with Dr Who then Arrow then finally Portlandia. I don't know, let me not confuse myself.
Many thanks for the help.
Malcolm Mutambanengwe
4,205 PointsOh, I am looking for good Java exercise material. I have good ebooks and all but I can't seem to find good exams. Right now I can't afford to pay for Java tests. I am still far away from even thinking about Java certification and all, though that is something I will do later.
If you know where I can find good exercises for practise send me the link(s).
May the force also be with your dog ;)
Steve Hunter
57,712 PointsFor learning resources, have you tried Lynda or Udemy? There may be something useful there.
As for the solution, you start wth a big chunk of JSON which has a name and then aa array of dictionary objects. Your loop starts with a counter at zero (the first element of an array) and goes on until it reaches the length of the array and stops. At each iteration, you access the 'title'
element which will return the value of the title, as you say, Doctor Who, Arrow then Portlandia.
Dog appreciates the force; although I do have 5 dogs but I know which one you mean.
Malcolm Mutambanengwe
4,205 PointsOh wow, 5 dogs.
I have three dogs, a cat, rabbits and roaches. LoL. Thanks for the links, will definitely check them out.
Steve Hunter
57,712 PointsFull roster:
5 x dogs 2 x parrots 4 x guinea pigs 12 x chickens
In the garden/field we have lots of wild deer & rabbits, pheasants, grouse & partridge as well as buzzards, kestrels, barn owls, marsh harriers, sparrowhawks.
Malcolm Mutambanengwe
4,205 PointsMy hands are held up high! Hahaha. I wouldn't mind some pheasants or grouse . . . . . . . . . . in a pie.
Steve Hunter
57,712 PointsI have the gun to do that; lots of pies!
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsNot sure .... not got time to look today, either. SOrry. I got as far as this:
So, I pulled the data out inside the loop and converted it to a string then put this in the Log.i. The chellenge isn't getting the output it expects. I don't know what it expects, though!
I'll have a look later/tomorrow.
Steve.