Last active
December 30, 2016 14:08
-
-
Save manishkpr/e3eba08c26b368740af1 to your computer and use it in GitHub Desktop.
Swift JSON Parsing From String
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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