Last active
August 29, 2015 14:16
-
-
Save josephlord/4b4c602b4102e046c830 to your computer and use it in GitHub Desktop.
Takes the LumaJSON parsing example and shows that it works directly on NSJSONSerialization result
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
import Foundation | |
var str = "Hello, playground" | |
let jsonStr = "{\"user\": {\"name\": \"jquave\",\"id\": 542,\"url\": \"http://jamesonquave.com\"},\"friend_ids\": [299,341,492],\"alert_message\": \"Please verify e-mail address to continue\"}" | |
var err:NSError? | |
let data = jsonStr.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) ?? NSData() | |
typealias JSONDict = [String:AnyObject] | |
let parsed = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.allZeros, error: &err) as? JSONDict | |
if let parsed = parsed {//LumaJSON.parse(jsonStr) { | |
// Simple printing to the console to check JSON structure | |
println(parsed) | |
// Simple Key/Value retreival | |
if let alertMessage = parsed["alert_message"] as? String { | |
println("Alert: \(alertMessage)") | |
} | |
// Nested JSON | |
if let userName = parsed["user"]?["name"] as? String { | |
println("Username is \(userName)") | |
} | |
// Nested object casting works using Swift's built-in mechanisms | |
if let friendIDs = parsed["friend_ids"] as? [Int] { | |
for friendID in friendIDs { | |
println("Friend ID: \(friendID)") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment