gpt4 book ai didi

ios - 如何在 `codable` 响应中使用字典?

转载 作者:行者123 更新时间:2023-11-28 12:14:43 28 4
gpt4 key购买 nike

我有以下来自 JSON 查询的响应。我如何将字典表示为可编码的?我缩短了 JSON 响应以节省空间。

{
"result":[
{
"delivery_address": "",
"made_sla": "true",
"watch_list": "",
"upon_reject": "cancel",
"location": {
"link": "https://foo.com/api/now/table/cmn_location/753ac76f4fd9320066bfb63ca310c79b",
"value": "753ac76f4fd9320066bfb63ca310c79b"
}
}
]
}

struct ResultList : Codable {
let result: [Result]
}

struct Result : Codable {
let delivery_address: String
let made_sla: String
let watch_list: String
let upon_reject: String
let location: Location
}

struct Location: Codable {
let link: String?
let value: String?
}

let decoder = JSONDecoder()
do {
let todo = try decoder.decode(ResultList.self, from: responseData)

print("todo \(todo)")

} catch {
print("error trying to convert data to JSON")
print(error)

}

我收到以下错误:

"Expected to decode Dictionary<String, Any> but found a string/data instead.", underlyingError: nil))

最佳答案

根据所有评论,我相信您实际解码的 JSON 看起来更像这样:

{
"result": [{
"delivery_address": "",
"made_sla": "true",
"watch_list": "",
"upon_reject": "cancel",
"location": {
"link": "https://foo.com/api/now/table/cmn_location/753ac76f4fd9320066bfb63ca310c79b",
"value": "753ac76f4fd9320066bfb63ca310c79b"
}
},
{
"delivery_address": "",
"made_sla": "true",
"watch_list": "",
"upon_reject": "cancel",
"location": ""

}
]
}

所以有些记录有一个位置,有些记录将位置编码为空字符串。基本上,这在很多层面上都把 JSON 搞得一团糟,无论生成什么代码,都应该修复它。坏消息我相信那不会发生。好消息是我们至少可以在本地修复它。

我们将不得不手动解码它,所以我们不妨在我们在这里的时候清理所有其他的烂摊子。首先是名称不符合 Swift 命名约定。我们可以使用 CodingKeys 来解决这个问题。我们还可以为当前输入错误的字符串提供真实类型(Bool 和 Rejection 枚举)。

enum Rejection: String, Codable {
case cancel
}
struct Result : Codable {
let deliveryAddress: String
let madeSLA: Bool
let watchList: String
let uponReject: Rejection
let location: Location?

private enum CodingKeys: String, CodingKey {
case deliveryAddress = "delivery_address"
case madeSLA = "made_sla"
case watchList = "watch_list"
case uponReject = "upon_reject"
case location
}
}

现在我们只需要能够解码它。请注意,我将 Location 设为可选。显然它有时不存在,所以您要么需要一个默认值,要么它需要是可选的。我选择了后者。解码所有这些非常简单:

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
deliveryAddress = try container.decode(String.self, forKey: .deliveryAddress)
madeSLA = try container.decode(String.self, forKey: .madeSLA) == "true"
watchList = try container.decode(String.self, forKey: .watchList)
uponReject = try container.decode(Rejection.self, forKey: .uponReject)

location = try? container.decode(Location.self, forKey: .location)
}

最后一行是您的实际问题。它只是说如果我们不能将它解码为 Location,则将其设置为 nil。我们可以在这里更严格,首先尝试将其解码为 Location,然后解码为 String,然后检查 String 是否为空,但对于任何解码失败,在这里使用 nil 感觉是合理的。

关于ios - 如何在 `codable` 响应中使用字典?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47145823/

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