Tal C Tal C -5 years ago 130
JSON Question

How do I choose an item from a nested array in json?

I am trying to get the JSon from the youtube trailers(ex. the name and source). How do I parse it exactly as I am doing something incorrectly while trying to pick the specific array. Here is my code:

public Void parseMovieExtraJson()
throws JSONException {
JSONObject jsonParentObject = new JSONObject(movieExtrasJSONString);
JSONArray trailerJSonArray = jsonParentObject.getJSONArray("trailers");
JSONArray youtubeTrailers = trailerJSonArray.getJSONArray(1);
for(int i = 0; i < youtubeTrailers.length(); i++){
JSONObject youtubeTrailerArray = youtubeTrailers.getJSONObject(i);
MovieTrailer movieTrailer = new MovieTrailer();
movieTrailer.setTrailerUrl("https://www.youtube.com/watch?v=" + youtubeTrailerArray.getJSONObject("source").toString());

return null;

And this is the JSON:

"adult": false,
"backdrop_path": "/nbIrDhOtUpdD9HKDBRy02a8VhpV.jpg",
"belongs_to_collection": null,
"budget": 58000000,
"genres": [],
"homepage": "http://www.foxmovies.com/movies/deadpool",
"id": 293660,
"imdb_id": "tt1431045",
"original_language": "en",
"original_title": "Deadpool",
"overview": "Based upon Marvel Comics’ most unconventional anti-hero, DEADPOOL tells the origin story of former Special Forces operative turned mercenary Wade Wilson, who after being subjected to a rogue experiment that leaves him with accelerated healing powers, adopts the alter ego Deadpool. Armed with his new abilities and a dark, twisted sense of humor, Deadpool hunts down the man who nearly destroyed his life.",
"popularity": 49.117553,
"poster_path": "/inVq3FRqcYIRl2la8iZikYYxFNR.jpg",
"production_companies": [],
"production_countries": [],
"release_date": "2016-02-09",
"revenue": 361000000,
"runtime": 108,
"spoken_languages": [],
"status": "Released",
"tagline": "Witness the beginning of a happy ending",
"title": "Deadpool",
"video": false,
"vote_average": 7.1,
"vote_count": 3840,
"releases": {},
"trailers": {
"quicktime": [],
"youtube": [
"name": "Official Red Band Trailer",
"size": "HD",
"source": "FyKWUTwSYAs",
"type": "Trailer"
"name": "Official Trailer",
"size": "HD",
"source": "ONHBaC-pfsk",
"type": "Trailer"
"name": "Official Trailer 2",
"size": "HD",
"source": "9vN6DHB6bJc",
"type": "Trailer"
"reviews": {
"page": 1,
"results": [
"id": "56c146cac3a36817f900d5f0",
"author": "huy.duc.eastagile",
"content": "A funny movie with a romantic love story.\r\n\r\nWade Wilson (Ryan Reynolds) is a former Special Forces operative who now works as a mercenary. His world comes crashing down when evil scientist Ajax (Ed Skrein) tortures, disfigures and transforms him into Deadpool. \r\n\r\nThe rogue experiment leaves Deadpool with accelerated healing powers and a twisted sense of humor. With help from mutant allies Colossus and Negasonic Teenage Warhead (Brianna Hildebrand), Deadpool uses his new skills to hunt down the man who nearly destroyed his life.",
"url": "https://www.themoviedb.org/review/56c146cac3a36817f900d5f0"
"id": "56ca035a9251414a7a0062f0",
"author": "Wong",
"content": "I actually enjoyed the movie so much that i'll recommend it to all my friends, at first i didn't really want to watch it because i'm not into super hero movies at all, but i did anyway, i mean people were talking so much about it i had to see it myself and what an awesome choice i made. The good thing about this movie is that Deadpool is a hero but in a very comedic way, you don't usually expect comedy from a superhero film but this one was full of comedy and the way they treated the plot was amazing, it was there, humor was there in every scene, even when there was fighting or romance or any other scene, the writers managed to add comedy everywhere in a very good way that'll surprisingly make you want to watch it again, and again. Thank you for taking the time read my review and if you're asking yourself if you should watch this movie, it's a definite Yes.",
"url": "https://www.themoviedb.org/review/56ca035a9251414a7a0062f0"
"id": "57375b8bc3a3687e1a0046bf",
"author": "Reno",
"content": "> So this guy in the red suit came and broke some records...\r\n\r\nI was not a Deadpool fan and I'm still not, but like any superhero films I'd pleasure watching it. The other reason was, we had other superheroes in our time, so this guy is kind of new to me. It was a stunning launch to the big screen for the Deadpool as well as for this new director. This film was merely an introduction tale than a fully stretched story. Like how a human becomes a mutant and later who seeks a revenge for ruining his natural life. But it yet to explain a couple of things and I hope it all will be revealed in its sequel.\r\n\r\nI am happy for Ryan Reynolds to get another chance, because his previous comic book character did not received well. I certainly liked the film, but nowadays there are plenty of superheroes to choose, in that, Deadpool's not in my top 5. So I am happy he joined the 'X-Men', because I never liked the boring 'X-Men' films or its characters, but I do love its spinoff 'Wolverine'. I'll definitely look forward for his solo films like 'Wolverine'.\r\n\r\nThis film broke a few records like the best box office so far for an 'X-Men' installment. A short film with the present and flashback narration shared the screen piece by piece alternatively. This was a strong R rated film so far I have seen in a superhero theme and there were plenty of other films referred. No offense to the people who liked it (and those who are going watch will do the same), but in my personal opinion it was a bit over-celebrated.\r\n\r\n6½/10",
"url": "https://www.themoviedb.org/review/57375b8bc3a3687e1a0046bf"
"total_pages": 1,
"total_results": 3

Answer Source

Trailers is a JSON object, not an array. You should use

JSONObject trailerJSonArray = jsonParentObject.getJSONObject("trailers");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download