我正在开发一个项目,我从网站上获取的JSON文件没有任何(密钥)供我开始解析。因此,当我在代码中使用该行时,let userInfo = swiftyJSON[].arrayValue
我将从JSON文件中获取所有信息。我的问题是我需要进一步解析它,所以我可以填写一个UITableView,我似乎无法解析它更具体。
这是我的JSON
中的代码let externalURL = "http://jsonplaceholder.typicode.com/users"
func getJSON(){
let url = URL(string: externalURL)
let request = NSURLRequest(url: url! as URL)
let session = URLSession(configuration:URLSessionConfiguration.default)
let task = session.dataTask(with: request as URLRequest) {(data, response, error) -> Void in
if error == nil {
let swiftyJSON = JSON(data: data!)
let userInfo = swiftyJSON[].arrayValue
print(userInfo)
}
else{
print("There was an error")
}
}
task.resume()
}
答案 0 :(得分:0)
一个起点:
let swiftyJSON = JSON(data: data!)
if let userInfo = swiftyJSON.array {
for user in userInfo {
print(user["name"].string, user["email"].string, user["phone"].string)
if let address = user["address"].dictionary {
print(address["city"]?.string)
}
}
}
所有印刷值均为选项。