0
我試圖從JSON中加載表中的數據。我想將JSON反序列化成一個數組。我使用稱爲Gloss的第三方庫,無論如何,它應該很容易,但我無法解決警告(與此主題相同)。從'Array <JSON>'(又名'Array <Dictionary <String, Any>>')轉換爲無關類型'JSON'(又名'Dictionary <String, Any>')總是失敗
這是JSON數據:http://prntscr.com/d8zdb5(這是一個有效的JSON,我已經檢查了它使用JSONLint)
這是我的代碼片段: 初始化從服務器
import Gloss
...
class CourierViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
struct courierList: Decodable{
let shipper: String?
let service: String?
let cost: Int?
let etd: String?
init?(json: JSON){
self.shipper = "shipper" <~~ json
self.service = "service" <~~ json
self.cost = "cost" <~~ json
self.etd = "etd" <~~ json
}
}
var TableData:Array<JSON> = Array<JSON>()
...
}
下載JSON:
...
override func viewDidLoad() {
super.viewDidLoad()
get_data_from_url(url: "https://www.imperio.co.id/project/ecommerceApp/CourierDataReq.php")
}
...
func get_data_from_url(url:String){
self.TableData.removeAll(keepingCapacity: false)
let parameterURL = ["dest":User_city.text!,"weight":String(CartManager.sharedInstance.totalWeightInCart())]
Alamofire.request(url, parameters: parameterURL).validate(contentType: ["application/json"]).responseJSON{ response in
switch response.result{
case .success(let data):
self.TableData.append(data as! JSON)
DispatchQueue.main.async(execute: {
self.tableView.reloadData()
})
break
case .failure(let error):
print("Error: \(error)")
break
}
}
}
使用該數組加載表格查看
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
guard let value = TableData as? JSON, //the warning appears in here
let eventsArrayJSON = value["ShipperResult"] as? [JSON]
else { fatalError() }
let CourierList = [courierList].from(jsonArray: eventsArrayJSON)
print((CourierList?.count)!)
return (CourierList?.count)!
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CourierCell", for: indexPath) as! CourierTableViewCell
if indexPath.row % 2 == 0 {
cell.backgroundColor = UIColor(red: 200.0/255.0, green: 255.0/255.0, blue: 255.0/255.0, alpha: 1.0)
} else {
cell.backgroundColor = UIColor(red: 135.0/255.0, green: 212.0/255.0, blue: 236.0/255.0, alpha: 1.0)
}
guard let value = TableData as? JSON, //The warning appears in here
let eventsArrayJSON = value["ShipperResult"] as? [JSON]
else { fatalError() }
let CourierList = [courierList].from(jsonArray: eventsArrayJSON)
for j in 0 ..< Int((CourierList?.count)!){
cell.label_shippingCourier.text = (CourierList?[j].shipper!)!
cell.label_shippingCourier.sizeToFit()
cell.label_serviceName.text = (CourierList?[j].service!)!
cell.label_serviceName.sizeToFit()
cell.label_estCost.text = String(describing: (CourierList?[j].cost!)!)
cell.label_estCost.sizeToFit()
if (CourierList?[j].etd! == "") {
cell.label_etd.text = "Data is not available"
} else {
cell.label_etd.text = (CourierList?[j].etd!)!
}
cell.label_etd.sizeToFit()
}
cell.selectionStyle = .none
return cell
}
任何想法如何解決問題?謝謝。
編輯: 我忘了提,我使用SWIFT 3.
錯誤消息說您嘗試將**數組**轉換爲**字典**,這是不可能的。 – vadian