gpt4 book ai didi

json - 解码 JSON 时出错 - keyNotFound(CodingKeys

转载 作者:行者123 更新时间:2023-11-30 10:44:34 25 4
gpt4 key购买 nike

当我尝试解码这个 json 时:

"polls": [
{
"title": "title",
"date": "date",
"summary": "summary",
"stats": {
"total": {
"dagegen gestimmt": 139,
"nicht beteiligt": 114,
"dafür gestimmt": 454,
"enthalten": 2
},
}
}, /*<and about 76 of this>*/ ]

使用此可编码:

struct poll: Codable {
var stats: stats
var title: String?
var date: String?
var summary: String?

struct stats: Codable {
var total: total
struct total: Codable {
var nays: Int
var yays: Int
var nas: Int
var abstentions: Int

private enum CodingKeys: String, CodingKey {
case yays = "dafür gestimmt"
case nays = "dagegen gestimmt"
case nas = "nicht beteiligt"
case abstentions = "enthalten"
}
}
}
}

我收到以下错误keyNotFound(CodingKeys(stringValue: "dagegen gestimmt", intValue: nil)(如果您需要完整的错误文本请告诉我)

我尝试了类似问题的一些答案,但没有任何效果。

最佳答案

显然,您出现了 total ,而 dagegen gestimmt 不存在。因此,将其设为可选,例如整数?:

struct Poll: Codable {
let stats: Stats
let title: String?
let date: Date?
let summary: String?

struct Stats: Codable {
let total: Total

struct Total: Codable {
let nays: Int?
let yays: Int?
let nas: Int?
let abstentions: Int?

private enum CodingKeys: String, CodingKey {
case yays = "dafür gestimmt"
case nays = "dagegen gestimmt"
case nas = "nicht beteiligt"
case abstentions = "enthalten"
}
}
}
}

我还建议以下内容,也反射(reflect)在上面:

  • 以大写字母开头类型名称(例如您的struct名称);
  • 使用 let 而不是 var,因为我们应该始终支持不变性,除非您确实要更改此 struct 中的这些值;和
  • 如果您的日期格式一致,我建议将 date 设为 Date 类型,然后您可以提供 JSONDecoder 匹配的 dateDecodingStrategy(参见下面的示例)。
<小时/>

例如:

let data = """
{
"polls": [
{
"title": "New Years Poll",
"date": "2019-01-01",
"summary": "summary",
"stats": {
"total": {
"dagegen gestimmt": 139,
"nicht beteiligt": 114,
"dafür gestimmt": 454,
"enthalten": 2
}
}
},{
"title": "Caesar's Last Poll",
"date": "2019-03-15",
"summary": "summary2",
"stats": {
"total": {
"dafür gestimmt": 42
}
}
}
]
}
""".data(using: .utf8)!

struct Response: Codable {
let polls: [Poll]
}

do {
let decoderDateFormatter = DateFormatter()
decoderDateFormatter.dateFormat = "yyyy-MM-dd"
decoderDateFormatter.locale = Locale(identifier: "en_US_POSIX")

let userInterfaceDateFormatter = DateFormatter()
userInterfaceDateFormatter.dateStyle = .long

let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .formatted(decoderDateFormatter)
let response = try decoder.decode(Response.self, from: data)
let polls = response.polls
for poll in polls {
print(poll.title ?? "No title")
print(" date:", poll.date.map { userInterfaceDateFormatter.string(from: $0) } ?? "No date supplied")
print(" yays:", poll.stats.total.yays ?? 0)
print(" nays:", poll.stats.total.nays ?? 0)
}
} catch {
print(error)
}

这会产生:

New Years Poll  date: January 1, 2019  yays: 454  nays: 139Caesar's Last Poll  date: March 15, 2019  yays: 42  nays: 0

关于json - 解码 JSON 时出错 - keyNotFound(CodingKeys,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56026110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com