如何使用 SwiftyJSON 获取正确的 JSON 响应?

How can I grab the correct JSON response using SwiftyJSON?

我正在使用 alamofire 发出 GET 请求并尝试使用 SwiftyJSON 转换为 JSON。我可以成功发出请求,但无法将响应转换为可用的 JSON 对象。

我正在尝试从 foursquare 地点详细信息响应中获取数据,并使用有关地点的适当信息更新 UITableView(详细信息页面)。 UITableView 有 3 个不同的部分。

这是我的 VenueDetailViewController (UITableViewController) 的 makeRequest 代码。

func makeRequest() {

    Alamofire.request(.GET, self.foursquareEndpointURL, parameters: [

        "client_id" : self.foursquareClientID,
        "client_secret" : self.foursquareClientSecret,
        "v" : "20140806"
        ])
        .responseJSON { (request, response, data, error) in
            println(request)
            println(response)
            println(error)
            println(data)
            if data != nil {
                var jsonObj = JSON(data!)
                // response is not in the form of an array? I think... thats why data is not setting .. I think "arrayValue" below is incorrect. should be something like dictionaryValue or stringValue
                if let obj = jsonObj["response"]["venue"].arrayValue as [JSON]? {
                    self.responseitems = obj
                    self.tableView.reloadData()
                    println("Objis: \(obj)")
                }

            }
    }
}

当我 println("Obis: \(obj)") 时,我得到 Objis: [] 的控制台输出,告诉我我从 JSON 响应中获取了错误的数据?这是 Foursquare Venue Detail API request

的 url

self.responseitemsvar responseitems:[JSON] = []

请帮忙!谢谢

查看此代码:

let url = "Foursquare Venue Detail API request goes here"
    Alamofire .request(.GET, url) .responseJSON(options: nil) { (_, _, data, error) -> Void in
        if error != nil {
            println(error?.localizedDescription)
        } else if let data: AnyObject = data {
            let jObj = JSON(data)
            if let venue = jObj["response"]["venue"].dictionary {
                println(venue)
            }

        }
    }

所以这里的主要区别是数据是字典,而不是数组。

顺便说一下,考虑在主队列中重新加载 table 视图,因为这会影响 UI:

dispatch_async(dispatch_get_main_queue()) {
    self.tableView.reloadData() // Update UI
}