Core Data & Decode:在 json 中更进一步



My Api(json( 看起来像这样:

"timezone": "America/Los_Angeles",
"currently": {
"time": 1534941429,
...
},

使用我的代码,我可以按如下方式访问时区:

timezone = try container.decode(Double.self, forKey: .timezone)

但是,如何深入了解 json 文件的层次结构并访问时间呢?

struct Currently: Codable {
let time: Int
...
}
let myStructDictionary = try container.decode([String: Currently].self, from: json)
myStructDictionary.forEach { print("($0.key): ($0.value)") }

希望这有帮助。

let currently = try container.nestedContainer(keyedBy: CodingKeys.self, forKey: .currently)
time = try currently.decode(Int.self, forKey: .time)

这就是它所需要的一切。希望这对任何人都有帮助。

最新更新