简体   繁体   English

Alamofire 5.5 responseDecodable 在 JSON 阵列上

[英]Alamofire 5.5 responseDecodable on JSON array

I am trying to decode an api response that just responds with an array.我正在尝试解码仅响应数组的 api 响应。 All the examples I see for using responseDecodable are a dictionary with a data key or something.我看到的所有使用 responseDecodable 的示例都是带有数据键或其他东西的字典。 But when I just pass [Movie].self into as the decoder I get:但是,当我将 [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

I'm assuming I'm not doing this right, looking for some help我假设我做的不对,寻求帮助

GET /request获取/request

Response:回复:

[{"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)
    }

According to the error, your JSON didn't return a name value, so check that's it's actually supposed to be that key, or mark the value as optional if it's not always returned.根据错误,您的 JSON 没有返回name值,因此请检查它实际上应该是那个键,或者如果它并不总是返回,则将该值标记为可选。 let name: String?

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

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