因此,我收到一些服务器的JSON数据,现在我试图将它保存为一个数组,所以我可以用它填充一个tableview,但在这里遇到麻烦是我的代码:如何将解析的json数据保存到一个数组与swift 3
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,以便我们知道我们处理的是什么数据。 – ovidiur
“但我得到错误”什么错误? – Larme
你想将这些数据存储在UserDefaults中? – shahnilay86