我正在尝试解析API JSON并在UITableView中显示它,但问题是我无法访问此API中的所有数组。
struct RootResults: Codable {
var results: [results]
}
// MARK: - results
struct results: Codable {
var container_number: String?
var commodities: [commodities]
}
// MARK: - commodities
struct commodities: Codable {
var commodity_name_en: String?
var varieties: [varieties]
}
// MARK: - varieties
struct varieties: Codable {
var variety_name_en: String?
var variety_name_ar: String?
}
import UIKit
class ViewController: UIViewController {
@IBOutlet var resultsTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
abuseedoAPIget()
}
var arrData = [results]()
var arrResults = [commodities]()
func abuseedoAPIget(){
let urlJSON = "http://abuseedotrading.com/apps/api/acp/?key=4ea1e08dd9ab329bbdaa9e5b42939c04&query=list_containers"
guard let url = URL(string: urlJSON) else {return}
URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let data = data else {return}
guard error == nil else {return}
do {
let decoder = JSONDecoder()
let APIResponse = try decoder.decode(RootResults.self, from: data)
self.arrData = APIResponse.results
DispatchQueue.main.async{
self.resultsTable.reloadData()
}
} catch let error {
print("Failed to decode JSON:", error)
}
}.resume()
}
}
extension ViewController: UITableViewDelegate, UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell
let dataa = arrData[indexPath.row]
cell.conLabel.text = dataa.container_number
cell.comLabel.text = dataa.commodity_name_en
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
}
为了使Swift能够将JSON响应解码为对象,您需要在Swift中使用对象定义类似的结构。
您的RootResults
对象需要实现Codable
协议并表示JSON结构。
在返回的JSON的一部分下面:
{
"status": 200,
"runtime": 1.7315270900726,
"results_count": 13,
"results": [
{
"container_id": 36473,
"container_number": "MMAU1163814",
"shipment_id": 17359,
}
}
RootResults
看起来像这样:
struct RootResults: Codable {
let status: Int
let runtime: Float
let results_count: 13
let results: [Container]
}
struct Container: Codable {
let container_id: Int
let container_number: String
let shipment_id: Int
}
有关Swift Codable的更多信息
在功能tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
中,您正在访问不同级别的数据。从container_number
中检索对象时,可以访问results
-属性。 commodity_name_en
是commodities
数组的更深层次和一部分。要访问commodities
-array中的第一项,请尝试以下操作:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell
let dataa = arrData[indexPath.row]
cell.conLabel.text = dataa.container_number
cell.comLabel.text = dataa.commodities[0].commodity_name_en
return cell
}
正如Vadian提到的,在Swift中以大写字母开始类型(结构,类,枚举)是很常见的。看看struct and classes文档