Learn How To Fetch And Parse JSON Data From A File

In this tutorial, we are going to create basic application called JSONDemo which will make you learn how to fetch and parse JSON data a file.JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language.

Here, we are going to do JSON parsing in swift. We are taking one JSON file named countries.json which we have provided in the code i have given.




      {“name”: “Afghanistan”,“code”: “AF”},

      {“name”: “Aland Islands”, “code”: “AX”},

      {“name”: “Albania”, “code”: “AL”},

      {“name”: “Algeria”, “code”: “DZ”},

      {“name”: “American Samoa”, “code”: “AS”},

      {“name”: “AndorrA”, “code”: “AD”},

      {“name”: “Angola”, “code”: “AO”},

      {“name”: “Anguilla”, “code”: “AI”},

      {“name”: “Antarctica”, “code”: “AQ”},

      {“name”: “Antigua and Barbuda”, “code”: “AG”},

      {“name”: “Argentina”, “code”: “AR”},

      {“name”: “Armenia”, “code”: “AM”},

      {“name”: “Aruba”, “code”: “AW”},

      {“name”: “Australia”, “code”: “AU”},

      {“name”: “Austria”, “code”: “AT”},

      {“name”: “Azerbaijan”, “code”: “AZ”},

      {“name”: “Bahamas”, “code”: “BS”},

      {“name”: “Bahrain”, “code”: “BH”},

      {“name”: “Bangladesh”, “code”: “BD”},

      {“name”: “Barbados”, “code”: “BB”},

      {“name”: “Belarus”, “code”: “BY”},

      {“name”: “Belgium”, “code”: “BE”},

      {“name”: “Belize”, “code”: “BZ”},

      {“name”: “Benin”, “code”: “BJ”},

      {“name”: “Bermuda”, “code”: “BM”},

      {“name”: “Bhutan”, “code”: “BT”},

here, country is the array which is having different indexes.We need to fetch name and code of the country and display it in the table view.

name and code are the keys of the dictionary.

now,we need to go through steps

Step 1:

we need to take tableView from object library as Shown below:

Screen Shot 2015-04-17 at 12.43.24 PM

Step 2:

put the table view in view controller and take the outlet from tableView to ViewController.swift

Screen Shot 2015-04-17 at 12.57.26 PM

We have given it the name as tvJSON.

@IBOutletweakvar tvJSON: UITableView!

write it in the ViewController.swift. One more thing give the data source and delegate from the viewcontroller to the table view.

Step 3:

Now take TableViewCell from the same object library as shown above:

Screen Shot 2015-04-17 at 1.06.29 PM

here, we have taken tableviewcell and we have to give the name of it in the class.

for creating class of the tableviewcell right click on the name of the project and you will se there new file, just click on that.

Screen Shot 2015-04-17 at 1.09.03 PM

you will see this

Screen Shot 2015-04-17 at 1.12.01 PM

click on CoCoa Touch Class and click on next…

Now your tableviewcell class is created.

Step 3:

Now we will go for the code..

In the ViewController.swift reload the tableview as shown below:

Screen Shot 2015-04-17 at 1.16.47 PM

jsonParsing() is the method we have created.

Step 4:

Screen Shot 2015-04-17 at 1.19.19 PM

Here we are fetching from cpuntries.json file. country is an array and arrDict is the dictionary which is having name and code as the key.

Step 5:

Screen Shot 2015-04-17 at 1.21.49 PM

These are the two delegate methods of tableview

Step 6:

Screen Shot 2015-04-17 at 1.23.09 PM

In this step we are given values in the label from the dictionary.

Its Done now:

Now Build and run the code. you will get the output….

Screen Shot 2015-04-17 at 1.29.12 PM