Javatpoint Logo
Javatpoint Logo

Parsing a Static JSON file in iOS application

In iOS applications, we often need to parse the JSON data, whether it is coming as a response or being saved as a local file on our device. In this article, we will discuss how we can parse a JSON from a local JSON file.

Consider the following Steps.

1. First, we need to store the drag the JSON file from our file system into XCode. We can also create a new file in XCode and save it with extension json, as shown below.

Parsing a Static JSON file in iOS application

Let's add the following JSON in the file.

2. We need to prepare a model for the above JSON; for this purpose, we need to look into every object of the JSON and decode the same in a separate class.

To create a model, create a new file in XCode as QuizResponseModel.swift and place the following code.

3. Now, add the following code in ViewController to parse the SampleJson.

Let's see whether we have received some data in our model or not. For this purpose, let's print the questions on the console.

Add the following code into viewDidLoad() method.

The ViewController.swift contains the following code.

Reading a JSON file (either local or on the web) is very easy with Swift. Here, we use the decodable protocol to decode the JSON response into our decodable model and use it anywhere in our code.






Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA