简体   繁体   English

如何从 responseDecodable 获取 Alamofire 响应 json?

[英]How to get Alamofire response json from responseDecodable?

I am wondering if there is a way to retrieve the response json from AF even if using the responseDecodable method:我想知道是否有办法从 AF 检索响应 json 即使使用 responseDecodable 方法:

AF.request(APIRouter.testGet).responseDecodable(decoder: jsonDecoder) { (response: DataResponse<[ObjectA], AFError>) in
            completion(response.result)
        }

I am asking this because even using the usual responseJSON it seems the JSON completion parameter is not missing.我问这个是因为即使使用通常的 responseJSON 似乎 JSON 完成参数也没有丢失。

response.data will contain the raw data you received. response.data将包含您收到的原始数据。 In the case of JSON, this can be converted to the JSON string when supplied with the right encoding:在 JSON 的情况下,当提供正确的编码时,可以将其转换为 JSON 字符串:

AF.request(APIRouter.testGet).responseDecodable(decoder: jsonDecoder) { (response: DataResponse<[ObjectA], AFError>) in
        if let data = response.data {
            print(String(data: data, encoding: .utf8)!)
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM