setting variable values from JSON

  1. 8 months ago

    Hi, I am trying to set variables from JSON values from the URL I am passing:

    https://api.tfl.gov.uk/Line/Route?app_id=6573044a&app_key=7a97df35339dc68625384077a5e07304
    here is my code:

    func thisisJSON(){
            let url=URL(string:"https://api.tfl.gov.uk/Line/Route?app_id=6573044a&app_key=7a97df35339dc68625384077a5e07304")
            let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
                if error != nil {
                    print("Error!")
                } else {
                    if let content = data {
                        do{
                            //Array
                            let myJSON = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
                            print(myJSON)
                        }
                        catch {
                            
                        }
                    }
                }
            }
            task.resume()
        }

or Sign Up to reply!