While developing many applications, you may have already come across iOS development, so while you are trying to access data using URL Session, this is where JSON files come in.
JSON in Swift is also known as SwiftyJSON. It is a library that helps to read and process JSON data from an API or server. Swift is strict about data types and wants the user to explicitly declare them, but JSON is usually implicit about data types. Hence, you need to learn how to parse JSON from files and URLs in Swift 4.2. Have a look at the process.
Open and Run Xcode and create a new project with File » New » Project ⌘⇧N. Select iOS » Application » Single View Application:
Here, there will be two methods to load JSON Data one way will be through the .json file which will be stored in out project folder and other way is to parse JSON from URL.
Now, create one global variable for JSON data type it’s “file” or “url”. For that put below line in ViewDidLoad() and will create a variable of NSMutableArray() type to store JSON Data:
To fetch JSON data, we have to copy the days.json file into our project repository. Which contains following JSON format data.
Here is the JSON that we will be parsing.
Following methods explains how to convert JSON file into NSData.
Then pass those data object in NSJSONSerialization method JSONObjectWithData:options:error. That should return array or dictionary as per JSON format.
func getFromFile() { let path: NSString = Bundle.main.path(forResource: "days", ofType: "json")! as NSString let data : NSData = try! NSData(contentsOfFile: path as String, options: NSData.ReadingOptions.dataReadingMapped) self.startParsing(data: data) }
Following method describes how to convert JSON file into NSData for web url JSON data:
By using following startParsing (data:) method,
We need to set delegate and datasource of TableView. Following line of code shows how to set delegate and data source for table view.
In ViewController.swift file write following line of code. You can set delegate and datasource from TableView by dragging TableView to viewcontroller icon also like below.
Select Project folder and do right click then select new file -> Cocoa Touch Class -> select UITableViewCell as subClass and give suitable name for file as shown below.
Your Custom Cell file is created and ready to use, we will create three Label outlet of to show data for movie name, description and lead role.
It should look like this:
Following below are the table view delegate methods that display parsed JSON formatted data.
Following line of code you need to put in ViewController.swift file.
numberOfSectionsInTableView(tableView : ) method is used to display sections.
Here we have only one section to display so return will be 1.
Cellforrowat method is used to show the data in tableview, we will use customCell file with identifier “cell”, then by using the dictionary key will get the data from dictionary and store it in particular variable and pass that variable in Label text.
This blog is wrapped with JSON parsing from files and URLs in Swift, which is also known as SwiftyJSON.
JSON is a readable format for structuring data that is used to transmit data between servers and web applications as an alternative to XML.JSON. This JavaScript-based language will be helpful to you while working with Swift.
CTO of Vasundhara Infotech, a leading Software development company in the USA. His technological interests has helped the company in making useful decisions.
Sign Up to our newsletter to get latest updates staight in your inbox.
Vasundhara respects your privancy. No Spam!
Sign Up to our newsletter to get latest updates staight in your inbox.
Vasundhara respects your privancy. No Spam!