2015-10-23 21 views
0

我真的很挣扎解析这个JSON(https://www.kimonolabs.com/api/7flcy3qm?apikey=gNq3hB1j0NtBdAvXJLEFx8JaqtDG8y6Y)和填充的tableview与“日期”文本,“事件”文本,“闪现”文字和“位置”文本。我意识到这对非Newb来说可能很容易。这是我大概可怕代码:斯威夫特JSON的tableview

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 
    } 
+0

您可能想分几步分割您的任务,并尝试一次解决每一步:1)提取您需要的数据在一个数据结构(元组数组)中,2)使用数据结构来填充你的表格单元格 – Lachezar

+0

请仔细解释这个更深入的例子吗?我是一个业余的 –

+0

例如尝试解析JSON文档并在元组数组中提取所需的值。您可以按照本教程学习如何在Swift中使用JSON:http://www.raywenderlich.com/82706/working-with-json-in-swift-tutorial;这里是关于元组的另一个教程:http://www.raywenderlich.com/115300/swift-2-tutorial-part-3-tuples-protocols-delegates-and-table-views – Lachezar

回答

0

有一个错字Collection2collection2但有在其中包含字典中的JSON字典的顶层关键results的主要问题collection2

…  
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:没有Date密钥collection2