get JSON values

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 getfromJSON(){
        let url = Foundation.URL(string: "https:/
        let request = NSMutableURLRequest(url: url! as URL)
      
        let task = URLSession.shared.dataTask(with: request as URLRequest) { data,response,error in guard error == nil && data != nil else {
                print("Error:",error)
                return
            }
            let httpStatus = response as? HTTPURLResponse
          
            if httpStatus?.statusCode == 200 {
                if data?.count != 0 {
                    let responseString = try! JSONSerialization.jsonObject(with: data!, options: .allowFragments)
                    print(responseString)
                } else {
                  
                }
            } else {
                print("error httpstatus code is: ", httpStatus!.statusCode)
            }
        }
        task.resume()
    }

Please help

Replies

can someone please help me here

can someone please help me here

Your question is about parsing JSON but the only code you’ve shown is networking code. You might have more luck if you described the format of the JSON and explained what values you’re trying to extract. You don’t actually need to show your networking code at all; just capture the JSON returned by your networking request, edit it down to a representative example, and post that.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"