Fetching data from JSON file in iOS

Posted By : Aditya Kumar Sharma | 29-Mar-2017

Recently I was working on one of my iOS apps which required to access JSON data which was kept in a file. We all can have this requirement to use data which is in JSON format (like Country Codes, Dial Codes, Pin codes, etc). So here it is.

We have to just follow these simple steps:

 

Firstly collect the data in JSON format and save it with an extension

 

 

For example I'm using Dialling Code JSON data. So I saved it with DialingCode.json extension. Add this file to the project. The data which this file include looks like this:

 

	{
    "countries":[
                 {
                 "name": "Afghanistan",
                 "dial_code": "+93",
                 "code": "AF"
                 },
                 {
                 "name": "Aland Islands",
                 "dial_code": "+358",
                 "code": "AX"
                 },
                 {
                 "name": "Albania",
                 "dial_code": "+355",
                 "code": "AL"
                 },
                 {
                 "name": "Algeria",
                 "dial_code": "+213",
                 "code": "DZ"
                 },
                 {
                 "name": "AmericanSamoa",
                 "dial_code": "+1684",
                 "code": "AS"
                 },
                 {
                 "name": "Andorra",
                 "dial_code": "+376",
                 "code": "AD"
                 }]
	}
 

 

Now its turn to fetch this data in our class. So move to view controller on which you want to access the data and make this method 

	  func getDiallingCodes() {
        //1
        if let path = NSBundle.mainBundle().pathForResource("DialingCode", ofType: "json") {
            do {
				//2
                let jsonData = try NSData(contentsOfFile: path, options: NSDataReadingOptions.DataReadingMappedIfSafe)
                do {
					//3
                    let jsonResult: NSDictionary = try NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
                    if let countries : [NSDictionary] = jsonResult["countries"] as? [NSDictionary] {
                        myCountriesDict = countries
                    }
                } catch let error as NSError {
                    print(error.localizedDescription)
                }
            } catch let error as NSError {
                print(error.localizedDescription)
            }
        }
    }	
 

 

Let me give you some overview that what we done in this method:

 

1. Using [NSBundle.mainBundle().pathForResource: ofType: ] we get file name, as it returns An array of full pathnames for the subpath or nil if no file located. 

2. Now in this we are pulling file content which  we require for our project.

3. Now we have to convert the NSData object to an NSDictionary, as we require JSON data. So you save it in an NSDictionary and can use for whatever you want.

 

That’s it!

Thanks

About Author

Author Image
Aditya Kumar Sharma

Aditya is a bright iOS developer, have knowledge of objective C, swift, swift 3, JSON, Core data and iPhone development. Apart from that he loves to travel and explore new things.

Request for Proposal

Name is required

Comment is required

Sending message..