Search code examples
arraysjsonswiftweather-apiopenweathermap

SWIFT access to JSON ARRAY


I have this data from JSON from OpenWeatherMap API

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

I have not idea how to access property "description"... I am using Alamofire and SwiftyJSON.

I have no problem get value from

"sys":{"type":1,"id":5091,"message":0.0237,"country":"GB","sunrise":1436673470,"sunset":1436732035}

using this piece of code:

var weatherJson = JSON(json!)
var temperature = weatherJson["main", "temp"].double
.
.
.
func setLabels() {

    if let temp = self.weather?.temp{
     //code
    }
}

but that don't work with extra brackets []...

EDIT: SOLUTION ->

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" for stored values

Solution

  • Try something like the following.

    var descriptionString = jsonObj["weather"]![0]["description"]