-2

我有来自 OpenWeatherMap API 的 JSON 数据

"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],

我不知道如何访问属性“描述”......我正在使用 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
  }
}
  • 存储值的“类天气”
4

1 回答 1

2

Try something like the following.

var descriptionString = jsonObj["weather"]![0]["description"]
于 2015-07-12T17:07:03.367 回答