Suppose you have a dictionary like
let dictionary = ["key1": "value1", "key2": "value2"]
To generate JSON data using the method you specified, just name it:
var jsonGenerationError: NSError? let jsonData = NSJSONSerialization.dataWithJSONObject(dictionary, options: .PrettyPrinted, error: &jsonGenerationError)
You can check if this works by analyzing the generated data:
var jsonParsingError: NSError? let parsedObject: AnyObject? = NSJSONSerialization.JSONObjectWithData(jsonData!, options: .AllowFragments, error:&jsonParsingError)
source share