【发布时间】:2020-07-23 01:13:40
【问题描述】:
所以我用我试图解析这些数据:
{"status":"success","data":{"city":"Sunnyvale","state":"California","country":"USA","location":{"type":"Point","coordinates":[-122.03635,37.36883]},"current":{"weather":{"ts":"2020-07-23T00:00:00.000Z","tp":25,"pr":1009,"hu":44,"ws":6.2,"wd":330,"ic":"02d"},"pollution":{"ts":"2020-07-23T00:00:00.000Z","aqius":7,"mainus":"p2","aqicn":2,"maincn":"p2"}}}}
我正在尝试获取aqius 结果以及tp...
这是我现在的代码,我已经创建了这些结构:
struct Response: Codable{
let data: MyResult
let status: String
}
struct MyResult: Codable {
let city: String
}
如您所见,我得到了 city,并且我可以确认它有效,因为当我收到请求和 print(json.data.city) 时,它会打印“Sunnyvale”。
但是我将如何获得其他值?我一直坚持如何在 location 、 current 和 pollution 数据结构中获取值,我该怎么做?
谢谢
【问题讨论】:
-
您可以使用“app.quicktype.io”从 Json 自动生成 Swift 模型。