How to work with response from Alamofire in Swift 4?

0

I have the following request that returns me a json I can not get the ulrs of live

Alamofire.request(urlRequest).validate().responseJSON { response in
                print(response.data)

                do {
                    if let data = response.data,
                        let json = try JSONSerialization.jsonObject(with: data) as? [String: Any],
                        let live = json["live"] as? [[String: Any]] {
                        for live in live {
                            if let livesUrl = live["live"] as? String {
                                self.urlLive.append(liveUrl)
                            }
                        }
                    }

                } catch {
                    print("Error deserializing JSON: \(error)")
                }
                print(self.urlLive)
            }

JSON REQUEST RETURNS

  

[       {           "id": "1",           "description": "TEST 1",           "live": "https: // .....",           "thumb": "https: // .....",           "online": true       },       {           "id": "2",           "description": "TEST 2",           "live": "https: // .....",           "thumb": "https: // .....",           "online": true       }]

    
asked by anonymous 10.01.2018 / 20:12

1 answer

-1

On return of function .responseJSON the response is already serialized. You just need to do:

Alamofire.request(url).validate().responseJSON { response in
    if let array = response.result.value as? [[String: Any]] {
        for item in array {
            let description = item["live"] as? String
            print(description)
        }
    }
}

link

    
07.05.2018 / 16:45