【问题标题】:Response Serialization error, how to fix it?响应序列化错误,如何解决?
【发布时间】: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() -&gt; Promise&lt;Response&lt;MyData&gt;&gt; => static func getData() -&gt; Promise&lt;[MyData]&gt; ?和responseDecodable(of: Response&lt;MyData&gt;.self => responseDecodable(of: [MyData].self?
  • 我解决了..谢谢你的帮助..)
  • 我也会尽快发布我的更新解决方案......这是一个小小的 st*pid err.. 大声笑
  • 顺便说一句,我认为问题仍然存在,我以为我已经解决了问题,但实际上,我似乎没有..可能是什么问题..?

标签: json swift alamofire promisekit


【解决方案1】:

首先您需要根据响应将quantity 的类型从Int 更改为String

 struct MyData: Codable {
     var id: Int
     var name: String
     var type: String
     var quantity: String
  }

然后你需要改变getData方法的签名如下,

static func getData() -> Promise<[MyData]> {
    let decoder = JSONDecoder()
    return Promise { seal in
        AF.request(API.getData, method: .get, parameters: .none).responseDecodable(of: [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)
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-30
    • 1970-01-01
    • 1970-01-01
    • 2017-11-17
    • 2021-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多