简体   繁体   English

没有与 Swift JSONDecoder 中的键 CodingKeys 关联的值

[英]No value associated with key CodingKeys in Swift JSONDecoder

I'm trying to parse the following response我正在尝试解析以下响应

{
"PaymentInfoList": [
{
  AccessTokens": [
  {
     "AccessToken": "d35c9c5d-60d3-7ea2-62f9-6754ed216d9f"
  }
  ],
"CloverPayment": true,
"SquarePayment": false,
}
]
}

Model模型

struct PaymentInfoList: Codable {
    let SquarePayment : Bool
    let CloverPayment : Bool
 }

Webservice网络服务

func fetchPaymentInfo(Id: String, completion:@escaping(PaymentInfoList)-> ()) {

   // Webservice URL & body 

  let session = URLSession.shared
            session.dataTask(with: request) { (data, response, error) in
                if let response = response {
                    print(response)
                }
                if let data = data {
                    
                    do {
             let parseResult = try! JSONDecoder().decode(PaymentInfoList.self, from: data)
                        DispatchQueue.main.async {
                          completion(parseResult)
                        }


                    } catch {
                        print(error)
                    }
                }
            }.resume()

}

Error:错误:

在此处输入图片说明

I couldn't understand where Im going wrong.我不明白我哪里出错了。 Plz guide请指导

Please do not accept the answer , would be too long to add comment hence adding it here, will delete it soon请不要接受答案,添加评论会太长,因此将其添加到此处,将很快删除

Use the following struct使用以下结构


struct Response: Codable {
    let paymentInfoList: [PaymentInfoList]

    enum CodingKeys: String, CodingKey {
        case paymentInfoList = "PaymentInfoList"
    }
}

struct PaymentInfoList: Codable {
    let cloverPayment, squarePayment: Bool

    enum CodingKeys: String, CodingKey {
        case cloverPayment = "CloverPayment"
        case squarePayment = "SquarePayment"
    }
}

Finally change JSONDecoder statement to最后将JSONDecoder语句更改为

let parseResult = try JSONDecoder().decode(Response.self, from: data)

You might as well change你也可以改变

                    DispatchQueue.main.async {
                        completion(parseResult.paymentInfoList)
                    }

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

相关问题 Swift 错误:keyNotFound 没有与键 CodingKeys 关联的值 - Swift Error: keyNotFound No value associated with key CodingKeys "没有与键 CodingKeys 关联的值" - No value associated with key CodingKeys Swift Parse JSON 错误:没有与键 CodingKeys 关联的值(stringValue:\\"_source\\ - Swift Parse JSON Error : No value associated with key CodingKeys(stringValue: \"_source\ 没有与键 CodingKeys 错误关联的值 - No value associated with key CodingKeys error JSON 解析错误 - 没有与键 CodingKeys 关联的值 - JSON parsing error - No value associated with key CodingKeys 没有与具有 Unsplash 的键 CodingKeys 关联的值 API - No value associated with key CodingKeys with Unsplash API 快速解析 JSON 时如何修复与键 CodingKeys 错误相关的没有值 - How to fix no value associated with key CodingKeys error when parsing JSON in swift 在Swift中使用JSONDecoder时,我不断收到“没有与键相关的值”错误。 有人可以解释这里出了什么问题吗? - I keep getting the “No value associated with key” error when using the JSONDecoder in Swift. Can someone please explain what is going wrong here? 如何修复CodingKeys的“无关联值”错误? - How to fix the “no value associated” error with CodingKeys? 在 Swift 4.0 中使用 JSONDecoder 解码数据键的特定值 - Decode particular value for key of Data using JSONDecoder in Swift 4.0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM