[英]Swift JSON tableview
I'm really struggling to parse this JSON ( https://www.kimonolabs.com/api/7flcy3qm?apikey=gNq3hB1j0NtBdAvXJLEFx8JaqtDG8y6Y ) and populate a tableview with the " Date " text, " Event " text, " Hasta " text and " Location " text. 我真的很难解析此JSON( https://www.kimonolabs.com/api/7flcy3qm?apikey=gNq3hB1j0NtBdAvXJLEFx8JaqtDG8y6Y ),并用“ 日期 ”文本,“ 事件 ”文本,“ 哈斯塔 ”文本和“ 位置 ”文字。 I realize this is probably easy for a non-newb. 我意识到这对于不熟悉它的人来说可能很容易。 Here's my probably terrible code: 这是我可能很糟糕的代码:
import UIKit
import Foundation
class MasterTableViewController: UITableViewController {
var vigoData = [String]()
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)
if let eventData = jsonResult["Collection2"] as? [[String: AnyObject]] {
for event in eventData {
if let _ = event["Event"] as? String {
self.vigoData.append("text")
}
}
}
print(self.vigoData)
} catch {
print("JSON Serialization failed")
}
}
}
task.resume()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 0
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 0
}
There is a typo Collection2
vs. collection2
but the main problem that there is a key results
at the top level of the JSON dictionary which contains the dictionary collection2
有一个错别字Collection2
与collection2
但是主要的问题是在包含字典collection2
的JSON字典的顶层有一个关键results
…
let jsonResult = try NSJSONSerialization.JSONObjectWithData(urlContent, options: NSJSONReadingOptions.MutableContainers)
let results = jsonResult["results"] as! [String: AnyObject]
if let collection2 = results["collection2"] as? [[String: AnyObject]] {
for eventData in collection2 {
print(eventData["Location"]!)
print(eventData["Hasta"]!)
if let event = eventData["Event"] as? [String: String] {
// self.vigoData.append("text")
print(event["text"]!)
}
}
}
…
PS: There is no Date
key in collection2
PS: collection2
没有Date
键
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.