【问题标题】:Swift JSON tableviewSwift JSON 表格视图
【发布时间】:2015-10-23 12:37:51
【问题描述】:

我真的很难解析这个 JSON (https://www.kimonolabs.com/api/7flcy3qm?apikey=gNq3hB1j0NtBdAvXJLEFx8JaqtDG8y6Y) 并用“日期”文本、“事件”文本、“Hasta”文字和“位置”文字。我意识到这对于非新手来说可能很容易。这是我可能很糟糕的代码:

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
  }

【问题讨论】:

  • 您可能希望将任务拆分为几个步骤并尝试一次修复每个步骤:1)在数据结构(元组数组)中提取您需要的数据,2)使用数据填写表格单元格的结构
  • 想通过示例更深入地解释这一点吗?我是业余爱好者
  • 例如尝试解析 JSON 文档并在元组数组中提取所需的值。您可以按照本教程学习如何在 Swift 中使用 JSON:raywenderlich.com/82706/working-with-json-in-swift-tutorial;这是另一个关于元组的教程:raywenderlich.com/115300/…

标签: ios json swift uitableview populate


【解决方案1】:

Collection2collection2 有一个错字,但主要问题是在 JSON 字典的顶层有一个键 results,其中包含字典 @​​987654324@

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多