【问题标题】:Getting 3 level deep JSON values with Alamofire 5.0 in Swift在 Swift 中使用 Alamofire 5.0 获取 3 级深度 JSON 值
【发布时间】:2019-07-23 07:42:11
【问题描述】:

Alamofire 5.0 出于某种原因没有为我提供纯 JSON,但给了我一个 json 文件结构的变体。截图如下。

我设法从 json 中检索到一两层深的数据,但由于某种原因,其他属性的结构不同。

我设法检索了 ma​​in 对象内的 temp 值,但我无法从 获取 description天气 对象。

这是我的代码 - nametemp 正常,description 来自天气不起作用。

【问题讨论】:

标签: ios json swift alamofire


【解决方案1】:

天气是一个数组,所以你应该像这样得到它:

if let weather = JSON["weather"] as? [[String: Any]],
   let firstWeather = weather.first {
    data.description = firstWeather["description"] as! String
}

【讨论】:

  • 太棒了!非常感谢!
【解决方案2】:

那是因为天气是一个 JSON 数组,你应该像这样检索它

weather[0]["description"]

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-01
    • 2015-09-26
    • 1970-01-01
    • 1970-01-01
    • 2022-11-01
    • 1970-01-01
    相关资源
    最近更新 更多