我有一个看起来像这样的json:
let jsonString = """
{
"data": [
{
"name": "Apple",
"price": 44,
"available": true
},
{
"name": "Pear",
"price": 27,
"available": false
}
],
"some_stuff": [],
"some_other_stuff": []
}"""
我的目标是解析data
数组的内容,特别是在不关心availibility
属性的结构中。我不在乎some_stuff
,而返回的some_other_stuff
就是那个json。
我创建了一个结构:
struct Fruit: Codable {
let name: String
let price: Int
}
还有上面的一种超级结构:
struct WholeResponse: Codable {
let data: [Fruit]
let someStuff: [Int?]
let someOtherStuff: [Int?]
enum CodingKeys: String, CodingKey {
case data
case someStuff = "some_stuff"
case someOtherStuff = "some_other_stuff"
}
struct Fruit: Codable {
let name: String
let price: Int
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let data = try container.nestedContainer(keyedBy: CodingKeys.self, forKey: .data)
name = try data.decode(String.self, forKey: .name)
price = try data.decode(Int.self, forKey: .price)
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
var data = container.nestedContainer(keyedBy: CodingKeys.self, forKey: .data)
try data.encode(name, forKey: .name)
try data.encode(price, forKey: .price)
}
}
但是在执行此操作后,我发现自己陷入了困境:
let jsonData = jsonString.data(using: .utf8)!
let jsonDecoder = JSONDecoder()
let fruits = try jsonDecoder.decode(WholeResponse.self, from: jsonData)
我认为我缺少有关数据表示的信息,可能会使事情复杂化,但是您能帮我解析此数据数组吗?
答案 0 :(得分:2)
首先,删除您不需要的所有内容:
struct WholeResponse: Codable {
let data: [Fruit]
struct Fruit: Codable {
let name: String
let price: Int
}
}
然后,像以前一样说
let response = try jsonDecoder.decode(WholeResponse.self, from: jsonData)
let fruits = response.data