4thSpace 4thSpace - 6 months ago 23
Swift Question

How to parse this with SwiftyJSON?

I'm using SwiftyJSON with NSURLSession.sharedSession().dataTaskWithURL and openweathermap.org. I'm getting back data and can pull out values. But I'm not having any luck trying to get "main" in the sample below.

Here's the first part of the JSON that comes back:

{
"cod" : 200,
"weather" : [
{
"id" : 800,
"description" : "clear sky",
"main" : "Clear",
"icon" : "01d"
}
],


if let data = data {
let json = JSON(data: data)
main = json["weather"]["main"].stringValue


main
doesn't contain anything.
json["weather"].stringValue
also doesn't contain anything. Any ideas what I'm doing wrong?

Answer

Try doing this to get the first item in the array:

json["weather"][0]["main"].stringValue

According to this line in SwiftyJSON documentation example:

json[1]["list"]

Is the way to get items in a JSON array

Comments