How to parse json data with Swift ?

Today, I found out that  reading the json data fron the local file is quite easy. All you have to do is like the sample I pasted below. The string value “myname” is the json files name located inside the folder. The variable “a” consists of the data within the file, and I have confirmed that the data had been retrieved by printing out the data.

    let path = NSBundle.mainBundle().pathForResource(“myname”, ofType: “json”)

        let jsonData = try? NSData(contentsOfFile: path!, options: NSDataReadingOptions.DataReadingMappedIfSafe)

        if let a = try? NSJSONSerialization.JSONObjectWithData(jsonData!, options: .AllowFragments) {



This is one of the basic way to obtain the necessary data from json,

but guess you can’t only use this code for more complicated json data.

Anyway I will see you in the next  post!

Leave a Reply

Your email address will not be published. Required fields are marked *