我正在测试如何为我正在开发的应用程序实现 JSON Web API。显然,SwiftyJSON 是一个非常棒的模块,我可以使用它来简化我的所有 JSON 调用。
到目前为止,我已经让调用代码正常工作,但至于打印输出,我仍然遇到问题。
我正在尝试返回“用户”索引下的所有数据,但是数组中有这么多数组,而且我是 JSON 新手,这一切都非常令人困惑
代码:
let urlPath = "http://api.randomuser.me/"
let url: NSURL = NSURL(string: urlPath)!
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(url, completionHandler: {data, response, error -> Void in
do {
let jsonResult = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
let json = JSON(jsonResult)
let count: Int? = json["results"].array?.count
print("found \(count!) result")
let results = json["results"]
for (key, subJson) in results{
if let title = subJson["user"]["gender"].string{
print(title)
}
}
} catch let error as NSError{
print("Something went wrong.")
print(error.localizedDescription)
}
})
task.resume()
随意访问 URL 以查看 JSON 数组的结构。
先感谢您
编辑:
我认为这可能有效:
for (key, subJson) in results{
for (key, user) in subJson{
print(user[key])
}
}
但是打印(用户 [键])= NULL。
有任何想法吗?