我从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))
答案 0 :(得分:2)
基于所有评论,我相信您实际解码的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
}
}
现在我们只需要能够解码它。请注意,我将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解决任何解码失败都是合理的。
答案 1 :(得分:0)
假设您的JSON是(请注意缺少的右括号)
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)
带
{{1}}