Created
April 23, 2016 00:02
-
-
Save SergioEstevao/cf3da326163306ddc1d1bc19b14df041 to your computer and use it in GitHub Desktop.
Load JSON with no lib
This file contains hidden or 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
do { | |
guard let url = NSBundle.mainBundle().URLForResource("sample", withExtension: "json"), | |
let JSONData = NSData(contentsOfURL: url), | |
let json = try NSJSONSerialization.JSONObjectWithData(JSONData, options: NSJSONReadingOptions()) as? Array<AnyObject> else | |
{ | |
print("Sample.json not found") | |
return | |
} | |
for item in json { | |
guard let user = item as? [String: AnyObject], | |
let id = user["id"] as? Int, | |
let name = user["name"] as? String, | |
let weight = user["weight"] as? Double else { | |
print("Unable to read json object") | |
return | |
} | |
print("\(id) : \(name)'s weight is \(weight)") | |
data.append("\(id) : \(name)'s weight is \(weight)") | |
} | |
} catch { | |
print(error) | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment