JSON Parsing Tutorial in iOS8 with Swift

JSON (JavaScript Object Notation) is a lightweight format, which is widely used to send data over the internet. The JSON format includes dictionaries and arrays. In this tutorial some data is requested from a web server and displayed on screen. This tutorial is built for iOS8 and Xcode6.

Open Xcode and create a new Single View Application. For product name, use iOS8SwiftJSONTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and make sure only iPhone is selected in Devices.

Go to the Storyboard. In Xcode 6 view controllers are now square by default, so that you can create one user interface for multiple devices. In iOS8 you can layout the Storyboard using constraints and size classes. Since this app is only going to be for the iPhone, you can disable size classes. In the File Inspector untick Use Size Classes.

Choose Keep size class data for: iPhone, and click Disable Size Classes.

Add 4 Labels to the Storyboard. Position and name the Labels according to the following screenshot.

Open the Assistant Editor and make sure the ViewController.swift file is visible. Ctrl + drag from the right date Label to the ViewController class and create the following Outlet.

Ctrl + drag from the right Time Label to the ViewController class and create the following Outlet.

JSONTest.com is a testing platform for services utilizing JavaScript Object Notation (JSON). In this case we make use of the date service. This will return the current date and time. Change the viewDidLoad method in.

 override func viewDidLoad() {
    // 1
    let urlAsString = "http://date.jsontest.com"
    let url = NSURL(string: urlAsString)!
    let urlSession = NSURLSession.sharedSession()
    let jsonQuery = urlSession.dataTaskWithURL(url, completionHandler: { data, response, error -> Void in
      if (error != nil) {
      var err: NSError?
      // 3
      var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as! NSDictionary
      if (err != nil) {
        println("JSON Error \(err!.localizedDescription)")
      // 4
      let jsonDate: String! = jsonResult["date"] as! String
      let jsonTime: String! = jsonResult["time"] as! String
      dispatch_async(dispatch_get_main_queue(), {
        dateLabel.text = jsonDate
        timeLabel.text = jsonTime
    // 5
  1. an NSURLSession is created with an NSURL object containing the url of the web service. This session is used for the networking calls.
  2. The dataTaskwithURL method creates the connection task which is going to be used to actually send the request. Note that it has a closure as it’s last parameter.
  3. With JSONObjectWithData:options:error method of the NSJSONSerialization class the actual parsing is performed. The parsed data will be written into a dictionary.
  4. The date and time values from the dictionary are assigned to the labels
  5. The resume method begins the web request.

Build and Run the project, the date and time values are written to the screen.

You can download the source code of the iOS8Swift..Tutorial at the ioscreator repository on Github