2016-11-18 33 views
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.

+0

错误消息说您尝试将**数组**转换为**字典**,这是不可能的。 – vadian

回答

0

@vadian非常感谢你对我的帮助,这是非常棘手的错误。

对于任何人也以某种方式来解决这个问题,答案是: “不要将数组强制转换为字典。”如果您需要创建空字典,请阅读本文Swift: declare an empty dictionary

相关问题