【发布时间】:2019-12-24 14:56:26
【问题描述】:
我尝试解码这种结构的 JSON:
“时间序列(5 分钟)”对象是一个对象字典,但当我加载 JSON 时字典的键发生变化时,我不知道如何使用 Codable 协议解码此 JSON。
我尝试编写一些模型,但每当我尝试访问字典时,我都会得到 nil。
struct stock: Decodable{
let function: Function?
enum CodingKeys: String, CodingKey {
case function = "Time Series (5min)"
}
}
struct Function: Decodable{
let values: [String:Value]
}
struct Value: Decodable{
let open: String
let heigh: String
let low: String
let close: String
let volume: String
enum CodingKeys: String, CodingKey{
case open = "1.open"
case heigh = "2. heigh"
case low = "3. low"
case close = "4.close"
case volume = "5.volume"
}
}
我怎样才能以一种我不需要事先知道密钥的方式编写我的代码,但也可以在最后得到它们以显示具有正确日期的数据。 谢谢
【问题讨论】:
-
也许这可以帮助你。 stackoverflow.com/questions/45598461/…
-
相关:Map JSON date keys to a struct in Swift 4 将字典映射到数组并将日期放入结构中