Swift Codable 预期解码 Dictionary<String, Any> 但发现 string/data
Swift Codable expected to decode Dictionary<String, Any>but found a string/data instead
我一直在使用 Codable
协议
这是我的 JSON
文件:
{
"Adress":[
],
"Object":[
{
"next-date":"2017-10-30T11:00:00Z",
"text-sample":"Some text",
"image-path":[
"photo1.png",
"photo2.png"
],
"email":"john.doe@test.com",
"id":"27"
},
{
"next-date":"2017-10-30T09:00:00Z",
"text-sample":"Test Test",
"image-path":[
"image1.png"
],
"email":"name.lastename@doe.com",
"id":"28"
}
]
}
我只需要关注对象数组,"image-path"数组可以包含0、1或2个字符串。
所以这是我的实现:
struct Result: Codable {
let Object: [MyObject]
}
struct MyObject: Codable {
let date: String
let text: String
let image: [String]
let email: String
let id: String
enum CodingKeys: String, CodingKey {
case date = "next-date"
case text = "text-sample"
case image = "image-path"
case email = "email"
case id = "id"
}
init() {
self.date = ""
self.text = ""
self.image = []
self.email = ""
self.id = ""
}
}
我以这种方式请求并获取 JSON 数据后,从我的服务 class 调用它:
if let data = response.data {
let decoder = JSONDecoder()
let result = try! decoder.decode(Result, from: data)
dump(result.Object)
}
除 image
属性
的 [String]
外,一切正常
但它无法编译,或者我收到 "Expected to decode..." 错误。
我应该如何处理 nil/no 数据场景?
我对你的 MyObject struct
做了一个小改动,即
1. 将所有 properties
标记为 optionals
2.删除了init()
(我觉得这里没有init()
的要求。)
3.在decoder.decode(...)
方法
中使用Result.self
代替Result
struct MyObject: Codable
{
let date: String?
let text: String?
let image: [String]?
let email: String?
let id: String?
enum CodingKeys: String, CodingKey
{
case date = "next-date"
case text = "text-sample"
case image = "image-path"
case email = "email"
case id = "id"
}
}
为了测试上面的内容,我使用了下面的代码,它工作正常。
let jsonString = """
{"Adress": [],
"Object": [{"next-date": "2017-10-30T11:00:00Z",
"text-sample": "Some text",
"image-path": ["photo1.png", "photo2.png"],
"email": "john.doe@test.com",
"id": "27"},
{"next-date": "2017-10-30T09:00:00Z",
"text-sample": "Test Test",
"image-path": ["image1.png"],
"email": "name.lastename@doe.com",
"id": "28"}
]
}
"""
if let data = jsonString.data(using: .utf8)
{
let decoder = JSONDecoder()
let result = try? decoder.decode(Result.self, from: data) //Use Result.self here
print(result)
}
这是我得到的结果值:
我一直在使用 Codable
协议
这是我的 JSON
文件:
{
"Adress":[
],
"Object":[
{
"next-date":"2017-10-30T11:00:00Z",
"text-sample":"Some text",
"image-path":[
"photo1.png",
"photo2.png"
],
"email":"john.doe@test.com",
"id":"27"
},
{
"next-date":"2017-10-30T09:00:00Z",
"text-sample":"Test Test",
"image-path":[
"image1.png"
],
"email":"name.lastename@doe.com",
"id":"28"
}
]
}
我只需要关注对象数组,"image-path"数组可以包含0、1或2个字符串。
所以这是我的实现:
struct Result: Codable {
let Object: [MyObject]
}
struct MyObject: Codable {
let date: String
let text: String
let image: [String]
let email: String
let id: String
enum CodingKeys: String, CodingKey {
case date = "next-date"
case text = "text-sample"
case image = "image-path"
case email = "email"
case id = "id"
}
init() {
self.date = ""
self.text = ""
self.image = []
self.email = ""
self.id = ""
}
}
我以这种方式请求并获取 JSON 数据后,从我的服务 class 调用它:
if let data = response.data {
let decoder = JSONDecoder()
let result = try! decoder.decode(Result, from: data)
dump(result.Object)
}
除 image
属性
[String]
外,一切正常
但它无法编译,或者我收到 "Expected to decode..." 错误。
我应该如何处理 nil/no 数据场景?
我对你的 MyObject struct
做了一个小改动,即
1. 将所有 properties
标记为 optionals
2.删除了init()
(我觉得这里没有init()
的要求。)
3.在decoder.decode(...)
方法
Result.self
代替Result
struct MyObject: Codable
{
let date: String?
let text: String?
let image: [String]?
let email: String?
let id: String?
enum CodingKeys: String, CodingKey
{
case date = "next-date"
case text = "text-sample"
case image = "image-path"
case email = "email"
case id = "id"
}
}
为了测试上面的内容,我使用了下面的代码,它工作正常。
let jsonString = """
{"Adress": [],
"Object": [{"next-date": "2017-10-30T11:00:00Z",
"text-sample": "Some text",
"image-path": ["photo1.png", "photo2.png"],
"email": "john.doe@test.com",
"id": "27"},
{"next-date": "2017-10-30T09:00:00Z",
"text-sample": "Test Test",
"image-path": ["image1.png"],
"email": "name.lastename@doe.com",
"id": "28"}
]
}
"""
if let data = jsonString.data(using: .utf8)
{
let decoder = JSONDecoder()
let result = try? decoder.decode(Result.self, from: data) //Use Result.self here
print(result)
}
这是我得到的结果值: