解析json时出错

时间:2018-06-25 13:15:15

标签: ios json swift parsing

我想发出获取请求,但出现错误。

public static func get_coupons(_ api_token: String,_ lang: String,_ completion: @escaping (_ code:Int?)-> Void) {

        APIHelper.instance.data.removeAll()

        let url = "\(APIHelper.BASE_URL + APIHelper.API.COUPONS.rawValue)"

        var code:JSON!

        let headers = [
            "apitoken": "\(api_token)"
        ]

        Alamofire.request(url, method: .get, headers: headers).responseJSON { (response) in
            switch response.result {
            case .success:
                if let json = JSON(rawValue: response.result.value!){

                    code = json["code"]

                    if code.intValue == 200 {

                        print("Done")

                    }else{
                        print(code.intValue)
                    }

                    completion(code.intValue)
                }
            case .failure(let error):
                print(error)
                return
            }
        }

    }

我尝试了上面的代码,但出现此错误。

  

responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(错误域= NSCocoaErrorDomain代码= 3840“字符0附近的无效值。” UserInfo = {NSDebugDescription =字符0附近的无效值。)))

0 个答案:

没有答案