lucgian841
lucgian841

Reputation: 2002

Parse a Json with Swift

I'm trying the new iOS language, but I'm getting issue. I will parse a JSON like the following:

{
   "coord":{
      "lon":-0.13,
      "lat":51.51
   },
   "sys":{
      "message":0.1947,
      "country":"GB",
      "sunrise":1404964557,
      "sunset":1405023356
   },
   "weather":[
      {
         "id":501,
         "main":"Rain",
         "description":"moderate rain",
         "icon":"10d"
      }
   ],
   "base":"cmc stations",
   "main":{
      "temp":291.87,
      "pressure":1018,
      "humidity":87,
      "temp_min":287.15,
      "temp_max":295.45
   },
   "wind":{
      "speed":3.1,
      "deg":350,
      "var_beg":320,
      "var_end":30
   },
   "rain":{
      "3h":0.5
   },
   "clouds":{
      "all":75
   },
   "dt":1404999902,
   "id":2643743,
   "name":"London",
   "cod":200
}

I got the JSON from a weather forecast service, until now I made the following method:

func getWeatherFinished(json:NSDictionary) {
    if (json != nil) {
        var weather: NSArray = json.objectForKey("weather") as NSArray
        var weatherDict: NSDictionary = weather.objectAtIndex(0) as NSDictionary
        var weatherConditions: NSString = weatherDict.objectForKey("description") as NSString

        var mainDict: NSDictionary = json.objectForKey("main") as NSDictionary
        var temp: NSString = mainDict.objectForKey("temp") as NSString
        var humidity: NSString = mainDict.objectForKey("humidity") as NSString
        var minTemp: NSString = mainDict.objectForKey("temp_min") as NSString
        var maxTemp: NSString = mainDict.objectForKey("temp_max") as NSString

        var windDict: NSDictionary = json.objectForKey("wind") as NSDictionary
        var speed: NSString = windDict.objectForKey("speed") as NSString
        var deg: NSString = windDict.objectForKey("deg") as NSString
    }
}

But when I run the app it crash and I don't know why. What's wrong in my code? Why it's crashing?

ERROR SCREENSHOT: enter image description here

When I get the JSON I wrote this code:

    func connectionDidFinishLoading(connection: NSURLConnection!) {

            var json: AnyObject! = NSJSONSerialization.JSONObjectWithData(receiveData, options: NSJSONReadingOptions(1), error: nil)
        if let jsonDict = json as? NSDictionary {
            getWeatherFinished(json as NSDictionary)
        }

}

Upvotes: 2

Views: 3010

Answers (1)

Connor
Connor

Reputation: 64684

Try checking to make sure your JSON is non-nil and can be a dictionary:

func connectionDidFinishLoading(connection: NSURLConnection!) {
    var json: AnyObject! = NSJSONSerialization.JSONObjectWithData(receiveData, options: NSJSONReadingOptions(1), error: nil)
    if let jsonDict = json as? NSDictionary{
        getWeatherFinished(json)
    }

}

This line is crashing because it the object with the key 'temp' is not an NSString.

var temp: NSString = mainDict.objectForKey("temp") as NSString

From your json it looks like it is a number, so you could try this instead:

var tempNum: NSNumber = mainDict.objectForKey("temp") as NSNumber
var temp: NSString = tempNum.stringValue()

Upvotes: 2

Related Questions