2017-02-25 157 views
0

我有一个像下面这样的JSON文件,我想解析它并填充我的tableview。swift - JSON字典解析

我想要得到的是 “材料”, “类别”, “product_types”

["facets": { 
     "material" : { 
     "data" : [ 
      { 
      "count" : 3, 
      "value" : "95% Polyester, 5% Spandex" 
      }, 
      { 
      "count" : 1, 
      "value" : "%100 Modal" 
      } 
     ], 
     }, 
     "categories" : { 
     "data" : [ 
      { 
      "id" : "7", 
      "name" : "test" 
      } 
     ], 
     }, 
     "product_types" : { 
     "data" : [ 
      { 
      "count" : 3, 
      "value" : "Sweatshirt" 
      }, 
      { 
      "count" : 1, 
      "value" : "Babet" 
      }, 
     ], 
     } 
    }] 

我的代码是:

var list: [String:JSON]? = [:] 
func loadList(){ 
ModafiliAPI.sharedInstance.refine(callback: { (refineList) in 
      if let data = refineList["facets"].dictionaryValue as [String:JSON]?{ 
       self.list = data 
       self.RefineTableView!.reloadData() 
       print(self.refineList!) 
      } 
     }) 
} 

我观察,我可以从访问 “面”打印输出。但在cellforrowat

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "RefineCell") as! RefineTableViewCell 
     cell.refineList = self.refineList?[indexPath.row] //-->>error 
     return cell 
    } 

我收到以下错误:Ambiguous reference to member 'subscript'

的UITableViewCell:

var refineList:[String:JSON]?{ 
     didSet{ 
      self.setupRefineList() 
     } 
    } 
+1

模型,则不应使用SwiftyJSON。这只是将JSON解析为* real *集合类型或自定义类/结构的绝妙工具。它会产生很多不必要的开销来反序列化表视图数据源和委托方法中的对象。并且不要将具体现有表视图的数据源数组声明为可选项。 – vadian

回答

0

的Xcode不知道是什么类型refineList,它的暧昧他

cell.refineList = self.refineList?[indexPath.row] as! [String:JSON] 

你也可以检查cell.refineList类型是什么?

相关搜索:

guard let list = refineList?[indexPath.row] else { 
     print ("Unexpected error !!") 
     return cell 
} 
cell.refineList:[String:JSON] = list as! [String:JSON] 
+0

用单元代码更新了我的问题。添加为! [字符串:JSON]和我得到相同的错误。 –

+0

您的手机上的refineList的类型是什么?你可以发送你的UITableViewCell代码,它可以是有帮助的。 – iLandes

+0

请检查我的问题的底部。 –