Hi, I'm trying to parse some json data received and I'm struggling to understand how to pull out and assign the nested values. I can manage the first two levels but when it comes any further I struggle.
I have an updateUI function which receives the data as String: AnyObject which works fine along with the first 2 levels of nesting.
What I'm trying to parse is the "forecastday" "day" array so that I can update the UILabels.
The current function is:
func updateUI(weatherData: [String: AnyObject]) {
let currentWeather = weatherData["current"]!
let forecast = weatherData["forecast"]!
let forecastDay = forecast["forecastday"] as AnyObject
let day = forecastDay["day"] as Any
print(day)
DispatchQueue.main.async {
self.windDirectionLabel.text = (currentWeather["wind_dir"] as! String)
}
}
My json data is:
{
"current":{
"last_updated_epoch":1484042228,
"last_updated":"2017-01-10 09:57",
"temp_c":6.0,
"temp_f":42.8,
"is_day":1,
"condition":{
"text":"Partly cloudy",
"icon":"//cdn.apixu.com/weather/64x64/day/116.png",
"code":1003
},
"wind_mph":23.0,
"wind_kph":37.1,
"wind_degree":290,
"wind_dir":"WNW",
"pressure_mb":1005.0,
"pressure_in":30.2,
"precip_mm":0.6,
"precip_in":0.02,
"humidity":93,
"cloud":0,
"feelslike_c":0.8,
"feelslike_f":33.5
},
"forecast":{
"forecastday":[
{
"date":"2017-01-10",
"date_epoch":1484006400,
"day":{
"maxtemp_c":8.1,
"maxtemp_f":46.6,
"mintemp_c":5.7,
"mintemp_f":42.3,
"avgtemp_c":5.9,
"avgtemp_f":42.6,
"maxwind_mph":22.4,
"maxwind_kph":36.0,
"totalprecip_mm":1.0,
"totalprecip_in":0.04,
"condition":{
"text":"Cloudy",
"icon":"//cdn.apixu.com/weather/64x64/day/119.png",
"code":1006
}
}
}]
}
}
} // The rest ommitted to preseerve space :)
Any help would be great.
Thanks