Skip to content

Instantly share code, notes, and snippets.

@scttymn
Created September 22, 2014 15:32
Show Gist options
  • Save scttymn/e021578857633372752e to your computer and use it in GitHub Desktop.
Save scttymn/e021578857633372752e to your computer and use it in GitHub Desktop.
Simple JSON Parse in Swift
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let urlPath = "http://www.telize.com/geoip"
let url = NSURL(string: urlPath)
let session = NSURLSession.sharedSession()
let task: Void = session.dataTaskWithURL(url, completionHandler: {data, response, error -> Void in
if (error != nil) {
println(error)
} else {
// Normally you would handle errors. Setting to nil for this example.
let jsonResult = NSJSONSerialization.JSONObjectWithData(
data,
options: NSJSONReadingOptions.MutableContainers,
error: nil) as NSDictionary
println(jsonResult)
println(jsonResult["country_code"])
}
}).resume()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment