【发布时间】:2020-10-07 14:33:23
【问题描述】:
我正在尝试解码一个 json 数据,但它会抛出一个错误:
[] nw_protocol_get_quic_image_block_invoke dlopen libquic failed responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.decodingFailed(error: Swift.DecodingError.typeMismatch(Swift.Dictionary<Swift.String, Any>, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Dictionary<String, Any> but found an array instead.", underlyingError: nil))))
我认为,我以正确的方式做到了,我也检查了类型不匹配,看起来正确,但无法理解为什么我仍然收到错误。 .
这是我的 json 数据响应的样子:
[
{
"id": 1,
"name": "BlackJet",
"type": "Silk",
"quantity": "100"
},
[
{
"id": 2,
"name": "Toto",
"type": "Leather",
"quantity": "10"
},
...,
]
这是我的结构数据:
import Foundation
struct Response<T: Codable>: Codable {
var data: [T]?
}
struct MyData: Codable {
var id: Int
var name: String
var type: String
var quantity: Int
}
和我的 serverCommunicator func:
static func getData() -> Promise<Response<MyData>> {
let decoder = JSONDecoder()
return Promise { seal in
AF.request(API.getData, method: .get, parameters: .none).responseDecodable(of: Response<MyData>.self, decoder: decoder) { response in
switch response.result {
case .success(let val):
return seal.fulfill(val)
case .failure(let err):
return seal.reject(err)
}
}
}
}
还有我在 VC didload 中的 apiCall 函数:
func getData() {
ServerCommunicator.getData().done { response -> Void in
guard response.data != nil, let data = response.data else {
print("Data could not be obtained.. .")
return
}
self.data = data
}.catch { (err) in
print(err)
}
}
注意:我的 api 中不存在 api 标头或参数//
【问题讨论】:
-
您的 JSON 中没有
data键。static func getData() -> Promise<Response<MyData>>=>static func getData() -> Promise<[MyData]>?和responseDecodable(of: Response<MyData>.self=>responseDecodable(of: [MyData].self? -
我解决了..谢谢你的帮助..)
-
我也会尽快发布我的更新解决方案......这是一个小小的 st*pid err.. 大声笑
-
顺便说一句,我认为问题仍然存在,我以为我已经解决了问题,但实际上,我似乎没有..可能是什么问题..?
标签: json swift alamofire promisekit