问题描述
2个问题
{"example2":13}
解决方法
这有意义吗?
根据需要,您可能希望将items
保留为字典,或者将其转换为数组。
let jsonString = """
{
"items": {
"1594647000": {
"date": "13-07-2020","open": 388.720000000000027284841053187847137451171875,"high": 394.18999999999999772626324556767940521240234375,"low": 388.3500000000000227373675443232059478759765625,"close": 392.76999999999998181010596454143524169921875
}
}
}
"""
struct Item: Decodable {
let open: Double
let close: Double
}
struct ItemsList: Decodable {
let items: [Item]
private enum CodingKeys: String,CodingKey {
case items
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let dict = try container.decode([String: Item].self,forKey: .items)
items = dict.map { $0.value }
}
}
let itemsList = try JSONDecoder().decode(ItemsList.self,from: jsonString.data(using: .utf8)!)