【问题标题】:how to save parsed json data into an array with swift 3如何使用swift 3将解析的json数据保存到数组中
【发布时间】:2018-02-12 15:03:39
【问题描述】:

所以我从服务器收到了一些 json 数据,现在我正在尝试将其保存为数组,以便我可以用它填充 tableview,但我的代码在这里遇到了麻烦:

class UserInfo : UIViewController{
var main = ""
session.dataTask(with: url) { (data, response, error) in
        if let response = response {
            print (response)
        }
        if let data = data {
            let json = (try? JSONSerialization.jsonObject(with: data, options: []))
            print(json)
            guard let array = json as? [Any] else {return}
            for info in array {
            guard let infoDict = info as? [String : Any] else{return}
            //there is a declared var called main
            //main is the one i want save as an array, currently its a variable. i tried to save it as an array by using as! Array but i get error
            self.main = infoDict["Title"] as! String
            print (self.main)
}
}
}.resume()
}

【问题讨论】:

  • 所以您需要发布收到的 json,以便我们知道我们在处理什么数据。
  • “但我得到错误”什么错误?
  • 您想将这些数据存储在 UserDefaults 中吗?
  • 可选(<__nsarrayi>( { 正文 = 21456; InfoID = 159; 标题 = "\U062d\U0633\U0627\U0628 \U0642\U062f\U0648\U0633 \U0641\U064a \U0624 U0648\U0644\U0644\U064a"; UserID = "db9b9dce-2c4d-413d-be2d-b61d01bbef3b"; infoType = 3; } //就是这样(接收到的数据)
  • 不,我只是想将它们保存为数组,以便在表格中查看它们

标签: ios arrays json swift3 tableview


【解决方案1】:

首先,您必须在请求范围之外声明ary。然后你必须将你的数据存储在同一个ary 中。

var ary: NSMutableArray = NSMutableArray()

session.dataTask(with: url) { (data, response, error) in
            if let response = response {
                print (response)
            }
            if let data = data {
                let json = (try? JSONSerialization.jsonObject(with: data, options: []))
                print(json)
                guard let array = json as? [Any] else {return}
                for info in array {
                    guard let infoDict = info as? [String : Any] else{return}
                    //there is a declared var called main
                    //main is the one i want save as an array, currently its a variable. i tried to save it as an array by using as! Array but i get error
                    self.main = infoDict["Title"] as! String
                    self.ary.add(self.main)
                    print (self.main)
                }
                print("Final array is :::",self.ary)
            }
            }.resume()

试试上面的代码。希望它对你有用。

【讨论】:

  • 工作正常,但我只是将“self”添加到这个“print(”Final array is :::",self.ary)"
  • @Duck.P 是的,很抱歉。因为它是一个闭包,我们正在请求从服务器获取数据。谢谢。
  • func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = UITableViewCell (style: UITableViewCellStyle.default, reuseIdentifier: "cell") cell.textLabel?.text = arry //当我这样做时 (cell.textlable?.text = arry) 我得到错误 (Cannot assign //value of type 'NSMutableArray' to type 'String?') return (cell) }
  • 是的,编译器会在运行时给你一个错误。因为您将值从NSMutableArray 分配给UILabel setText 属性。你不能这样做。虽然如果要分配值,则需要强制数组而不是标签。就像下面的代码:- cell.ary = arry cell.ary 表示您在 UITableViewCell 类实例中的 NSMutableArray。 arry 表示您已解析的数据容器。
猜你喜欢
  • 2018-03-19
  • 2017-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多