在本教程的上一部分中,无涯教程讨论了使用Alamofire进行获取请求。无涯教程创建了一个项目,在其中使用tableview在应用程序中显示艺术家的信息。
在本教程的这一部分中,无涯教程将通过创建响应模型来扩展该项目,并在响应模型中解析响应数据。
要创建响应模型,无涯教程需要通过Command + N短键创建一个新的swift文件,然后选择swift文件。
ArtistResponseModel类应该是Decodable的基类。
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? }
要在AlamoFire API请求中解析响应,无涯教程将使用JSondeCoder,该对象是从JSON对象中解码数据类型的实例的对象。
JSondecoder的解码方法用于解码JSON响应。它返回无涯教程指定的类型的值,从JSON对象解码。说法如下。
func decode<T>(T.Type, from: Data) -> T
在这里,无涯教程将通过ArtistResponseModel和响应数据的实例。说法如下。
do{ let result: ArtistResponseModel = try JSONDecoder().decode(ArtistResponseModel.self, from: response.data!) } catch{ }
它将返回ArtistResponseModel的实例,现在包含解析的JSON响应。
ViewController.swift文件具有以下代码。
链接:https://www.learnfk.comhttps://www.learnfk.com/ios/ios-parsing-json-response.html
来源:LearnFk无涯教程网
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)") do{ 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){ do{ 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 } }
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)