Skip to content

Instantly share code, notes, and snippets.

@bjallen
Last active August 29, 2015 14:05
Show Gist options
  • Save bjallen/7432eabe2e391ec29a44 to your computer and use it in GitHub Desktop.
Save bjallen/7432eabe2e391ec29a44 to your computer and use it in GitHub Desktop.
dealing with json in swift
import Foundation
let response = "[{\"name\": \"value\", \"age\": 30}, {\"name\": \"value2\", \"age\": 50}]"
let data: NSData! = response.dataUsingEncoding(NSUTF8StringEncoding)
var options = NSJSONReadingOptions.AllowFragments
var serializationError: NSError?
let json: AnyObject! = NSJSONSerialization.JSONObjectWithData(
data,
options: nil,
error: &serializationError
)
var items = json as Array<Dictionary<String, AnyObject>>
for item in items {
var name = item["name"]! as String
var age = item["age"]! as Int
println("\(name) - \(age)")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment