我的Api(json)看起来像这样:
"timezone": "America/Los_Angeles",
"currently": {
"time": 1534941429,
...
},
使用我的代码,我可以按以下方式访问时区:
timezone = try container.decode(Double.self, forKey: .timezone)
但是如何挖掘json文件的层次结构并访问时间?
答案 0 :(得分:0)
struct Currently: Codable {
let time: Int
...
}
let myStructDictionary = try container.decode([String: Currently].self, from: json)
myStructDictionary.forEach { print("\($0.key): \($0.value)") }
Hope this helps.
答案 1 :(得分:0)
let currently = try container.nestedContainer(keyedBy: CodingKeys.self, forKey: .currently)
time = try currently.decode(Int.self, forKey: .time)
这就是它所需要的。希望这对任何人都有帮助。