Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Swift JSON Parsing From String
let jsonString = "{\"id\":123,\"Name\":\"Munish\"}"
// convert String to NSData
var data: NSData =jsonString.dataUsingEncoding(NSUTF8StringEncoding)!
var error: NSError?
// convert NSData to 'AnyObject'
var jsonObject: AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.allZeros, error: &error)
println("Error: \(error)")
let id = (jsonObject as! NSDictionary)["id"] as! Int
let name = (jsonObject as! NSDictionary)["name"] as! String
println("Id: \(id)")
println("Name: \(name)")
Json Parsing, Swift JSON Parsing, Swift JSON parsing from string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment