【发布时间】:2015-10-23 17:13:59
【问题描述】:
我已经成功地用下面的代码调用了一个 JSON URL。如何从输出中提取属性?我有 Date、Event、Hasta 和 Location 属性。
这是 JSON 结构和 XCode 输出:
https://www.kimonolabs.com/apis/7flcy3qm#data
这是我的快速代码
class MasterTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
splitViewController!.preferredDisplayMode = UISplitViewControllerDisplayMode.AllVisible
UINavigationBar.appearance().barTintColor = UIColor(red: 52.0/255.0, green: 170.0/255.0, blue: 220.0/255.0, alpha: 1.0)
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()]
let url = NSURL(string:"https://www.kimonolabs.com/api/7flcy3qm?apikey=gNq3hB1j0NtBdAvXJLEFx8JaqtDG8y6Y")!
let task = NSURLSession.sharedSession().dataTaskWithURL(url) { (data, response, error) -> Void in
if let urlContent = data {
do {
let jsonResult = try NSJSONSerialization.JSONObjectWithData(urlContent, options: NSJSONReadingOptions.MutableContainers)
print(jsonResult)
} catch {
print("JSON Serialization Failed")
}
}
}
task.resume()
}
如何提取属性?非常感谢
【问题讨论】:
-
不注册就看不到链接的“whatever”。顺便说一句,喜欢所有的空白行。 ;-)
-
jsonResult是字典或数组。您需要转换它(例如if let jsonResult = jsonResult as? [String : AnyObject]),然后您可以在该条件转换范围内访问其成员,例如jsonResult["someKey"] -
谢谢亚伦!我看看我有没有运气!
-
对不起,我不知道该怎么做哈哈!有人可以为我写一个坚定的例子吗? :)
标签: json xcode swift parsing tableview