JSON String to NSDictionary with Swift

后端 未结 3 1610
孤城傲影
孤城傲影 2021-02-09 01:50

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:41

    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()
    

提交回复
热议问题