我从一个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))发布于 2017-11-07 01:37:28
基于所有的评论,我相信您实际上正在解码的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和拒绝枚举)。
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
}
}现在我们只需要能够破译它。注意,我将位置设置为可选的。显然,有时它不存在,所以您要么需要一个默认值,要么需要它是可选的。我选择了后者。解码所有这些都是非常直接的:
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似乎是合理的。
发布于 2017-11-06 21:29:21
假设您的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)https://stackoverflow.com/questions/47145823
复制相似问题