【发布时间】:2022-01-28 14:21:26
【问题描述】:
我正在尝试解码仅响应数组的 api 响应。我看到的所有使用 responseDecodable 的示例都是带有数据键或其他东西的字典。但是当我只是将 [Movie].self 作为解码器传递给我时:
failure(Alamofire.AFError.responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.decodingFailed(error: Swift.DecodingError.keyNotFound(CodingKeys(stringValue: "name", intValue: nil), Swift.DecodingError.Context(codingPath: responseDecodable
我假设我做的不对,寻求帮助
获取/request
回应:
[{"id": 1, "name": "test"}]
struct Movie: Codable, Hashable {
let id: Int
let name: String
}
AF.request("http://localhost/request", parameters: parameters)
.responseDecodable(of: [Movie].self) { response in
debugPrint(response)
}
【问题讨论】:
-
它应该可以工作,但这真的是你的 JSON 吗?你能做
print("Response String: \(String(data: response.data!, encoding:.utf8)")并显示输出吗? -
debugPrint(response)应该包括接收到的 JSON 的完整输出,除非它非常大。在调试器中,我喜欢使用po String(decoding: response.data!, as: UTF8.self) as NSString来打印 JSON,因为它避免了String的转义。