如何在 `codable` 响应中使用字典?
How to use a dictionary in `codable` response?
我从 JSON 查询中得到以下响应。我如何将字典表示为可编码的?我已缩短 JSON 响应以保存 space。
{
"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://blah/blah/foo",
"value": "fsfdfr32r34rwfwffas"
}
}
]
}
你可以解码这些结构
struct Root : Decodable {
let result : [Result]
struct Result : Decodable {
let location: Location
}
}
struct Location: Decodable {
let link: String
let value: String
}
和
JSONDecoder().decode(Root.self, from: data)
根据所有评论,我相信 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 感觉是合理的。
我从 JSON 查询中得到以下响应。我如何将字典表示为可编码的?我已缩短 JSON 响应以保存 space。
{
"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://blah/blah/foo",
"value": "fsfdfr32r34rwfwffas"
}
}
]
}
你可以解码这些结构
struct Root : Decodable {
let result : [Result]
struct Result : Decodable {
let location: Location
}
}
struct Location: Decodable {
let link: String
let value: String
}
和
JSONDecoder().decode(Root.self, from: data)
根据所有评论,我相信 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 感觉是合理的。