Skip to content

Instantly share code, notes, and snippets.

@SergioEstevao
Created April 23, 2016 00:02
Show Gist options
  • Save SergioEstevao/cf3da326163306ddc1d1bc19b14df041 to your computer and use it in GitHub Desktop.
Save SergioEstevao/cf3da326163306ddc1d1bc19b14df041 to your computer and use it in GitHub Desktop.
Load JSON with no lib
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