【问题标题】:SwiftyJson array in arraySwiftyJson 数组中的数组
【发布时间】:2018-05-15 13:48:06
【问题描述】:

我使用 Alamofire 和 SwiftyJson 从服务器解析 JSON。我在 tableView 中显示它,然后当我点击一行时,我会在网络上打开一个链接。

但我无法解析数组。

array`[
    {
        "title": " ",
        "likes": 0,
        "post_id": 60050,
        "image": "",
        "image1": "",
        "image2": "",
        "data": "",
        "arguments": [
            "link",
            "url"
        ],
        "provider": "custom",
        "price": " usd",
        "desc": "",
        "shop": ""
    },`

我的要求:

Alamofire.request("").responseJSON(completionHandler: { response in
    if ((response.result.value) != nil) {
        let swifts = JSON(response.result.value!)

        if let resData = swifts.arrayObject{
        }
        self.dataTable = resData as! [[String:AnyObject]]
    }

这显示文本:

cell.titleLabel.text = indexData["arguments "] as? String

但加载时出现错误:

Swift._SwiftDeferredNSArray 0x600000228cc0>( url

【问题讨论】:

  • 数组中的dict。
  • 我需要做什么?
  • 我没有完全清楚你到底想做什么。如果你重写你的 json 和一些你想显示的代码,那就更好了。它有助于我理解。
  • 我尝试解析“参数” - 到 tableView 并显示它并在视图之间移动
  • 你能告诉我你的 Json 的正确结构,以便我可以帮助你。

标签: ios json swift alamofire


【解决方案1】:

在这里,我假设您完美地从 API 获得响应。现在像这样解析数据。

  1. 创建一个全局变量var arrData : [Any] = []

  2. 现在像这样调用 api 并获取数据。

    if ((response.result.value) != nil) {
        self.arrData = (response.result.value) // here your all response in arrData. 
        self.tblView.reloadData() // reload tableview
    }
    else{
        // you didn't get the data
    }        
    
  3. 现在在 TableView DataSource

    • numberOfRowsInSection

      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
         return arrData.count 
      }
      
    • cellForRow

      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          let cell = tableView.dequeueReusableCell(withIdentifier: "your_cell_id") as! YourCell
      
          let aDictValue = arrData[indexPath.row] as? [String : Any]
      
          cell.labelTitle.text = aDictValue["title"] as! String
          cell.labelProvider.text = aDictValue["provider"] as! String
      
          // set your data like this.
      
          return cell
      }
      

如果还有问题可以问。

【讨论】:

    【解决方案2】:

    尝试像那样解析它

    let array = swifts["arguments"].arrayValue 
    
    for item in array 
    {
        print(item)
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-18
      • 2016-09-30
      • 2016-01-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多