0

I have having trouble parsing out the similar videos title. There will always be either 1 or 2 similar movies in the json so I am trying to store both of those videos in two separate arrays.

Here is my JSON example here:

 {
        "status": "ok",
        "status_message": "Query was successful",
        "data": {
            "limit": 20,
            "movies": [
                {
                    "id": 400,
                    "url": "www.google.com",
                    "title": "Battle for Skyark",
                    "year": 2016,
                    "rating": 2.8,
                    "runtime": 88,
                    "genres": [
                        "Action",
                        "Adventure",
                        "Family",
                        "Sci-Fi"
                    ],
                    "summary": "When a mysterious race of creatures takes over a desolate Earth, humanity must take refuge in SkyArk, the city in the sky. After the rebellion against a corrupt leadership fails, the wealthy doom the rebels' children to live on the ruins of the old Earth. The rebel leader's son, Rags, must lead his fellow exiles against the monsters in order to have a chance to return to SkyArk, but he soon finds that he has a much greater purpose in saving humanity.",
                    "language": "English",
                    "background_image": "background.jpg",
                    "state": "ok",
                    "similarvideos": [
                        {
                            "title": "The Long Road"
                        },
                        {
                            "title": "Boyhood"
                        }
                    ],
                    "date_uploaded": "2015-10-31 21:48:19",
                    "date_uploaded_unix": 1446342499
                }
            }
        }

I am able to to parse out summaries like this (along with other elements):

var summarys = [String]()
self.summarys = json.valueForKeyPath("data.movies.summary") as! [String]

This is how I am trying to parse the similar videos title and it is not working:

self.similarfirstmovies = json.valueForKeyPath("data.movies.similarvideos[0].title") as! [String]
self.similarsecondmovies = json.valueForKeyPath("data.movies.similarvideos[1].title") as! [String]

All help is much appreciated.

2 Answers 2

1

You can get your similar videos like this :

var similarfirstmovie = json.valueForKeyPath("data.movies[0].similarvideos[0].title") as! String
var similarsecondmovie = json.valueForKeyPath("data.movies[0].similarvideos[1].title") as! String

your mistake is movies is array. firstly you should select which movie you want. then you can reach the similar videos of that movie.

But if you want to get all similarfirstmovies and secondmovies from json directly.First of all you should get movies object in an array then you should write a predicate for reaching all similarvideos.

Sign up to request clarification or add additional context in comments.

Comments

1

At my first glance, your json isn't correct,"movies": [ is an array should with a ']' somewhere, but seems not.

similarvideos is an array, so parse it as an array first, something like

         let movies = json.valueForKeyPath("data.movies.similarvideos")
         movies.forEach { movie in
            print(movie["title"])
        }

should work.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.