Alamofire: Bagaimana menangani respons json di Swift 3 | xcode 8 | alamofire 4.0

Dibuat pada 30 Sep 2016  ·  3Komentar  ·  Sumber: Alamofire/Alamofire

Alamofire.request("https://httpbin.org/get").responseJSON { respon di
debugPrint(respons)

            if let json = response.result.value {
                let ip = json["url"] as! String
            }
        }

Kesalahan: Ketik 'Apa saja' tidak memiliki anggota subskrip

support

Komentar yang paling membantu

Beginilah cara saya selalu menangani respons JSON saya.

.responseJSON {
    response in

    switch response.result {
    case .failure(let error):
        // Do whatever here
        return

    case .success(let data):
        // First make sure you got back a dictionary if that's what you expect
        guard let json = data as? [String : AnyObject] else {
            NSAlert.okWithMessage("Failed to get expected response from webserver.")
            return
        }

        // Then make sure you get the actual key/value types you expect
        guard var points = json["points"] as? Double,
            let additions = json["additions"] as? [[String : AnyObject]],
            let used = json["used"] as? [[String : AnyObject]] else {
                NSAlert.okWithMessage("Failed to get data from webserver")
                return
        }

Semua 3 komentar

            if response.result.value is NSNull {
                return
            }
            let JSON = response.result.value as? NSDictionary
            let id = JSON?["id"] as! String

Saya pikir Anda membutuhkan? sebelum ["url"]

Beginilah cara saya selalu menangani respons JSON saya.

.responseJSON {
    response in

    switch response.result {
    case .failure(let error):
        // Do whatever here
        return

    case .success(let data):
        // First make sure you got back a dictionary if that's what you expect
        guard let json = data as? [String : AnyObject] else {
            NSAlert.okWithMessage("Failed to get expected response from webserver.")
            return
        }

        // Then make sure you get the actual key/value types you expect
        guard var points = json["points"] as? Double,
            let additions = json["additions"] as? [[String : AnyObject]],
            let used = json["used"] as? [[String : AnyObject]] else {
                NSAlert.okWithMessage("Failed to get data from webserver")
                return
        }

Terima kasih @grosch! 🍻

Apakah halaman ini membantu?
0 / 5 - 0 peringkat