How to read JSON using Swift 2.0

ViewController.swift

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        guard let path = NSBundle.mainBundle().pathForResource("test", ofType: "json") else {
            print("Error finding file")
            return
        }

        do {
            let data: NSData? = NSData(contentsOfFile: path)
            if let jsonResult: NSDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary {
                let dataDictionary = jsonResult["list"] as! NSDictionary
                let total = dataDictionary["total"]
                print("total items : \(total!)");
                let itemArray = dataDictionary["item"] as! NSArray
                for item in itemArray {
                    let group = item["group"]!
                    let offset = item["offset"]!
                    let name = item["name"]!
                    let ndbno = item["ndbno"]!
                    print("Item \(offset!): \(group!) - \(name!) - \(ndbno!)")
                }
            }
        } catch let error as NSError {
            print("Error:\n \(error)")
            return
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

 

Series Navigation<< How to make REST API calls & parse JSON with Swift 2.0
Collection View with Animations >>