JSON文件以数组开头怎么办?

What should I do if a JSON file begins with an array?

所以我正在尝试使用 Swifty-JSON 来浏览 JSON 文件。我遇到的问题是 JSON 文件(来自 twitter API)以数组开头,示例如下:

[
    {
        "created_at": "Tue Mar 31 13:47:02 +0000 2015",
        "id": 582901921171796000,
    }
]

假设我想阅读 created_at 部分,我会执行以下操作:

let urlPath = "http://api.twitter.com/1.1/statuses/home_timeline.json?AUTHENTICATION_INFO"
let url: NSURL = NSURL(string: urlPath)!
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(url, completionHandler: {data, response, error -> Void in

        if error != nil {
            // If there is an error in the web request, print it to the console
            println(error.localizedDescription)
        }

        var err: NSError?
        var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSDictionary
        if err != nil {
            // If there is an error parsing JSON, print it to the console
            println("JSON Error \(err!.localizedDescription)")
        }

        let json = JSON(jsonResult)

        let createdAt = json[0]["created_at"].string!
        println(createdAt)

    })
    task.resume()

虽然这不起作用,但我退休了,并收到一条错误消息,该消息突出显示了我的代码,并在控制台中打印了 (lldb)

有谁知道如何在 Swifty 中做到这一点-JSON,它可能很简单,但我不确定。

谢谢

在您的 NSJSONSerialization 中将响应转换为 NSArray:

var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSArray