Home » iOS Parsing JSON Response

iOS Parsing JSON Response

by Online Tutorials Library

Parsing JSON Response

In the previous section of this tutorial, we discussed making get request using Alamofire. We had created a project in which we used tableview to display the artist’s information in the application.

In this section of the tutorial, we will extend that project by creating a response model, and we will parse the response data in the response model.

To create the response model, we need to create a new swift file by command + n short key and choose the swift file.

Parsing JSON Response

The ArtistResponseModel class should be a base class of Decodable in swift.

ArtistResponseModel.swift

To parse the response in Alamofire API request, we will use JSONDecoder, which is an object that decodes instances of a data type from JSON objects.

The decode method of JSONDecoder is used to decode the JSON response. It returns the value of the type we specify, decoded from a JSON object. The syntax is given below.

Here, we will pass the instance of ArtistResponseModel and response data. The syntax is given below.

It will return the instance of ArtistResponseModel, which now contains the parsed json response.

The ViewController.swift file has the following code.

ViewController.swift


You may also like