如何在XCODE中过滤JSON响应?

时间:2016-02-27 20:17:43

标签: ios json xcode swift

情况: 我有一个通过SwiftyJSON解析的JSON响应。我正在用数据填充表格单元格。一切都很酷,但现在我需要从我的JSON中删除一些字典,并且它不允许我以任何方式(循环或其他条件)执行此操作。

收到的JSON示例:

{"post-comments" : [
              {
                "post" : "new",
                "_id" : "1",
              },
              {
                "post" : "new",
                "_id" : "2",
              },
              {
                "post" : "post with title",
                "_id" : "23",
              },
              {
                "post" : "new",
                "_id" : "29",
              },
              {
                "post" : "post with title",
                "_id" : "90",
              },
              {
                "post" : "post with title",
                "_id" : "33",
              }
            ]

}

我试着去掉一些字典,让我们说“post”===“new” - 我需要从我的JSON中删除它们并继续用左数据填充我的单元格。

完全卡住..任何想法将不胜感激。

这是完整的tableviewcontroller:

        class TableViewContr: UITableViewController {

        let Comments : String = "http://localhost:3000/api/comments/"

        var json : JSON = JSON.null

        override func viewDidLoad() {
            super.viewDidLoad()

            getPostComments(Comments)

        }

        func getPostComments(getcomments : String) {

            Alamofire.request(.GET, getcomments).responseJSON {
                response in

                guard let data = response.result.value else {
                    return
                }

                self.json = JSON(data)

                self.tableView.reloadData()

            }

        }

        override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

            switch self.json["post-comments"].type {

            case Type.Array:
                return self.json["post-comments"].count

            default:
                return 1

            }
        }

        func populateFields(cell: TableViewContrCell, index: Int) {

            guard let comment = self.json["post-comments"][index]["post"].string else {
                return
            }
            cell.commentContent!.text = comment

        }

        override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TableViewContrCell

            populateFields(cell, index: indexPath.row)

            return cell
        }


    }

1 个答案:

答案 0 :(得分:0)

这是一种在重新加载表视图之前解析和过滤JSON数据的方法。

comments是包含post-comments字典

的数据源数组
class TableViewContr: UITableViewController {

  let commentURL : String = "http://localhost:3000/api/comments/"

  var comments = [[String:String]]()

  override func viewDidLoad() {
    super.viewDidLoad()
    getPostComments(commentURL)
  }

  func getPostComments(getcomments : String) {

    Alamofire.request(.GET, getcomments).responseJSON {
      response in

      guard let data = response.result.value else { return }

      let jsonData = JSON(data)
      let postComments = jsonData["post-comments"].arrayObject as! [[String:String]]
      self.comments = postComments.filter{$0["post"] != "new"}
      dispatch_async(dispatch_get_main_queue()) {
        self.tableView.reloadData()
      }
    }
  }

  override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return comments.count
  }

  override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TableViewContrCell

    let comment = comments[indexPath.row]
    cell.commentContent!.text = comment["post"]!

    return cell
  }
}