如何使用新的可解码协议解码此 JSON?
How to decode this JSON with the new decodable protocol?
问题
我试图解码我的 JSON 文件,以便将其转换为本机结构类型。显然它不起作用,我不知道为什么。我复制了结构中的所有 JSON 对象,以便可解码协议可以完成它的工作。
JSON 文件
JSON 文件可以在这里找到:https://pastebin.com/hZxmDMue
模型(结构)
这些模型反映了 JSON 文件中完全相同的对象。
struct Appstructure: Decodable {
var data: [Subdata]
}
struct Subdata: Decodable {
var favourites: Items
var cart: Items
}
struct Items: Decodable {
var items: [Item]
}
struct Item: Decodable {
var type: String
var content: Content
}
struct Content: Decodable {
var productLines: [String]
var productImage: String
}
JSON解析
我正在使用苹果提供的JSON解码器,如下所示:
func fetchAppStructure() -> Appstructure? {
guard let path = Bundle.main.path(forResource: "iMessage-test-version-3", ofType: "json") else { return nil }
guard let data = try? Data(contentsOf: URL(fileURLWithPath: path), options: []) else { return nil }
do {
try JSONDecoder().decode(Appstructure.self, from: data)
} catch {
print(error.localizedDescription)
}
return nil
}
错误
但是我收到以下解码错误。
DecodingError
▿ keyNotFound : 2 elements
- .0 : CodingKeys(stringValue: "cart", intValue: nil)
▿ .1 : Context
▿ codingPath : 2 elements
- 0 : CodingKeys(stringValue: "data", intValue: nil)
▿ 1 : _JSONKey(stringValue: "Index 0", intValue: 0)
- stringValue : "Index 0"
▿ intValue : Optional<Int>
- some : 0
- debugDescription : "No value associated with key CodingKeys(stringValue: \"cart\", intValue: nil) (\"cart\")."
- underlyingError : nil
If anything else is needed to reproduce this bug please ask. It is probably something small in logical thinking. Hopefully somebody sees the issue! Thanks in advance.
你的结构有误。
子数据有 favourites
或 cart
,不能同时有。
使 favourites
和 cart
可选。
struct Subdata: Decodable {
var favourites: Items?
var cart: Items?
}
问题
我试图解码我的 JSON 文件,以便将其转换为本机结构类型。显然它不起作用,我不知道为什么。我复制了结构中的所有 JSON 对象,以便可解码协议可以完成它的工作。
JSON 文件
JSON 文件可以在这里找到:https://pastebin.com/hZxmDMue
模型(结构)
这些模型反映了 JSON 文件中完全相同的对象。
struct Appstructure: Decodable {
var data: [Subdata]
}
struct Subdata: Decodable {
var favourites: Items
var cart: Items
}
struct Items: Decodable {
var items: [Item]
}
struct Item: Decodable {
var type: String
var content: Content
}
struct Content: Decodable {
var productLines: [String]
var productImage: String
}
JSON解析
我正在使用苹果提供的JSON解码器,如下所示:
func fetchAppStructure() -> Appstructure? {
guard let path = Bundle.main.path(forResource: "iMessage-test-version-3", ofType: "json") else { return nil }
guard let data = try? Data(contentsOf: URL(fileURLWithPath: path), options: []) else { return nil }
do {
try JSONDecoder().decode(Appstructure.self, from: data)
} catch {
print(error.localizedDescription)
}
return nil
}
错误
但是我收到以下解码错误。
DecodingError
▿ keyNotFound : 2 elements
- .0 : CodingKeys(stringValue: "cart", intValue: nil)
▿ .1 : Context
▿ codingPath : 2 elements
- 0 : CodingKeys(stringValue: "data", intValue: nil)
▿ 1 : _JSONKey(stringValue: "Index 0", intValue: 0)
- stringValue : "Index 0"
▿ intValue : Optional<Int>
- some : 0
- debugDescription : "No value associated with key CodingKeys(stringValue: \"cart\", intValue: nil) (\"cart\")."
- underlyingError : nil
If anything else is needed to reproduce this bug please ask. It is probably something small in logical thinking. Hopefully somebody sees the issue! Thanks in advance.
你的结构有误。
子数据有 favourites
或 cart
,不能同时有。
使 favourites
和 cart
可选。
struct Subdata: Decodable {
var favourites: Items?
var cart: Items?
}