gpt4 book ai didi

ios - 如何从子JSON中获取数据

转载 作者:行者123 更新时间:2023-11-28 13:50:54 26 4
gpt4 key购买 nike

我正在制作一个天气应用程序,我正试图从我儿子的文件中获取日期和天气。我成功进入“daily”,但无法进入“daily”中的“data”。这是代码:

func downloadForecastWeather(completed: @escaping DownloadComplete) {
Alamofire.request(FORECAST_API_URL).responseJSON { (response) in
let result = response.result
if let dictionary = result.value as? Dictionary<String, AnyObject> {
if let list = dictionary["list"] as? [Dictionary<String, AnyObject>] {
for item in list {
let forecast = ForecastWeather(weatherDict: item)
self.forecastArray.append(forecast)
}
self.forecastArray.remove(at: 0)
self.tableView.reloadData()
}
}
completed()
}
}

这是我的 ForecastWeather 类:

init(weatherDict: Dictionary<String, AnyObject>) {
if let temp = weatherDict["temperatureMin"] as? Double {

let rawValue = (temp - 273.15).rounded(toPlaces: 0)
self._temp = rawValue
}


if let date = weatherDict["time"] as? Double {

let rawDate = Date(timeIntervalSince1970: date)
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium

self._date = "\(rawDate.dayOfTheWeek())"
}

}

这是 json 的链接: https://api.darksky.net/forecast/132d3013b6e07bdf66159c1f5a90f76c/37.8267,-122.4233所以基本上我的问题是如何从“每日”中的“数据”获取数据并在我的 for in 方法中使用它。

最佳答案

尝试与 SwiftyJSON 一起使用,它很容易集成到您的代码中,您所要做的就是在 downloadForecastWeather 函数中添加以下代码。

func downloadForecastWeather(completed: @escaping DownloadComplete) {
Alamofire.request(FORECAST_API_URL).responseJSON { (response) in

let jsondata = JSON(response.result.value!)

if let myData = jsondata["list"]["daily"]["data"].arrayObject { // here you can access your data inside daily
for data in myData {
let forecast = ForecastWeather(weatherDict: data)
self.forecastArray.append(forecast)
}
self.forecastArray.remove(at: 0)
self.tableView.reloadData()
}
completed()
}
}

更多详情请引用此Documentation .

希望对你有用。

: D

关于ios - 如何从子JSON中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54622888/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com