SWIFT 访问 JSON 数组
SWIFT access to JSON ARRAY
我从 OpenWeatherMap JSON API
获得了这些数据
"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],
我不知道如何访问 属性 "description"...
我正在使用 Alamofire 和 SwiftyJSON.
我没问题从
获取价值
"sys":{"type":1,"id":5091,"message":0.0237,"country":"GB","sunrise":1436673470,"sunset":1436732035}
使用这段代码:
var weatherJson = JSON(json!)
var temperature = weatherJson["main", "temp"].double
.
.
.
func setLabels() {
if let temp = self.weather?.temp{
//code
}
}
但这不适用于额外的括号 []
...
编辑:解决方案 ->
func getWeatherData(urlString: String) {
var weatherJson = JSON(json!)
var description = weatherJson["weather"][0]["description"].stringValue
}
func setLabels() {
if let description = self.weather?.desc{
self.descriptionLabel.text = description
}
}
- "class weather" 存储值
尝试如下操作。
var descriptionString = jsonObj["weather"]![0]["description"]
我从 OpenWeatherMap JSON API
获得了这些数据"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],
我不知道如何访问 属性 "description"... 我正在使用 Alamofire 和 SwiftyJSON.
我没问题从
获取价值"sys":{"type":1,"id":5091,"message":0.0237,"country":"GB","sunrise":1436673470,"sunset":1436732035}
使用这段代码:
var weatherJson = JSON(json!)
var temperature = weatherJson["main", "temp"].double
.
.
.
func setLabels() {
if let temp = self.weather?.temp{
//code
}
}
但这不适用于额外的括号 []
...
编辑:解决方案 ->
func getWeatherData(urlString: String) {
var weatherJson = JSON(json!)
var description = weatherJson["weather"][0]["description"].stringValue
}
func setLabels() {
if let description = self.weather?.desc{
self.descriptionLabel.text = description
}
}
- "class weather" 存储值
尝试如下操作。
var descriptionString = jsonObj["weather"]![0]["description"]