JSON String to NSDictionary with Swift

后端 未结 3 1443
一生所求
一生所求 2021-02-09 02:11

I am trying to create a dictionary from data that is held in a server, I receive the data but I cannot convert the data to an NSDictionary, I believe it is held in

3条回答
  •  礼貌的吻别
    2021-02-09 02:37

    Here jsonResult will give you the response in NSDictionary:

    let url = NSURL(string: path)
            let session = NSURLSession.sharedSession()
            let task = session.dataTaskWithURL(url!, completionHandler: {data, response, error -> Void in
                println("Task completed")
                if(error != nil) {
                    // If there is an error in the web request, print it to the console
                    println(error.localizedDescription)
                }
                var err: NSError?
                var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSDictionary
                if(err != nil) {
                    // If there is an error parsing JSON, print it to the console
                    println("JSON Error \(err!.localizedDescription)")
                }
    
            })
            task.resume()
    

提交回复
热议问题