Facebook Login Integration in iOS Google Sign-in integration in iOS How to capture the image using the iOS app How to make a phone call in the iOS app Paytm SDK Integration in iOS Razorpay Integration in iOS UIAlertController in Swift Using SQLite in iOS app Creating a real-time chat application using Firebase Using Realm database in iOS app iOS: machine learning What is CoreML Using Advanced CoreML Access Control in Swift iOS Memory Management in iOS applications Protocol Oriented Programming in iOS Swift Initialization in Swift Parsing a Static JSON file in iOS application Sending Email using the iOS application Using AVAudioPlayer to play sounds in iOS applications Search Bar in iOS Applications UIImage in iOS UIBarButtonItem in iOS Working with UIBezierPath in iOS applications SFSafariViewController in iOS Transforming Views with CGAffineTransform UIActivityViewController in iOS Using UIColor to customize app appearance iOS Data Recovery Apps

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.

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

ArtistResponseModel.swift

import Foundation class ArtistResponseModel:Decodable{ public var resultCount:Int? public var results:[Results]? class Results : Decodable{ public var wrapperType : String? public var kind : String? public var artistId : Int? public var collectionId : Int? public var trackId : Int? public var artistName : String? public var collectionName : String? public var trackName : String? public var collectionCensoredName : String? public var trackCensoredName : String? public var artistViewUrl : String? public var collectionViewUrl : String? public var trackViewUrl : String? public var previewUrl : String? public var artworkUrl30 : String? public var artworkUrl60 : String? public var artworkUrl100 : String? public var collectionPrice : Double? public var trackPrice : Double? public var releaseDate : String? public var collectionExplicitness : String? public var trackExplicitness : String? public var discCount : Int? public var discNumber : Int? public var trackCount : Int? public var trackNumber : Int? public var trackTimeMillis : Int? public var country : String? public var currency : String? public var primaryGenreName : String? public var isStreamable : Bool?

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.

func decode<T>(T.Type, from: Data) -> T

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

let result: ArtistResponseModel = try JSONDecoder().decode(ArtistResponseModel.self, from: response.data!) catch{

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

The ViewController.swift file has the following code.

ViewController.swift

import UIKit import Alamofire class ViewController: UIViewController { @IBOutlet weak var tableView: UITableView! var artist = Array<Results>() override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. loadJsonData() tableView.delegate = self tableView.dataSource = self //tableView.rowHeight = UITableView.automaticDimension func loadJsonData() Alamofire.request("https://itunes.apple.com/search?media=music&term=bollywood").responseJSON { (response) in //print("Response value \(response)") if(response.result.isSuccess){ let result: ArtistResponseModel = try JSONDecoder().decode(ArtistResponseModel.self, from: response.data!) debugPrint(result) self.artist = result.results ?? [] self.tableView.reloadData() }catch{ extension ViewController : UITableViewDataSource{ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return artist.count func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "MainTableViewCell") as! MainTableViewCell if(artist.count > 0){ let artistData = artist[indexPath.row] cell.artistImgView.image = try UIImage(data: Data(contentsOf: URL(string: artistData.artworkUrl60!) ?? URL(string: "http://www.google.com")!)) cell.trackName.text = artistData.trackName cell.artisName.text = artistData.artistName cell.artistCountry.text = artistData.country }catch{ return cell extension ViewController : UITableViewDelegate{ func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 220 Click Here to Download Project