[英]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.