json - SwiftyJSON : How can I add token?

标签 json swift swifty-json

我正在使用 API 并通过 SwiftyJSON 获取 json 数据。我需要 API 的添加 token 。我如何使用 SwiftyJson 来做到这一点?

我的代码:

let jsonData = (NSData(contentsOfURL: NSURL(string: "http://api.football-data.org/v1/soccerseasons/424/leagueTable ")!)! as NSData)

    var readableJSON = JSON(data: jsonData, options: .MutableContainers, error: nil)

    let name = readableJSON["standings"]

通常,当我使用 Swift 的 JSON 时,我会使用此代码添加 token :

    let url = NSMutableURLRequest(URL: NSURL(string: "http://api.football-data.org/v1/soccerseasons/424/leagueTable")!)
    url.addValue("mytokenishere", forHTTPHeaderField: "X-Auth-Token")
    url.HTTPMethod = "GET"

最佳答案

您是否正在使用此数据进行发布/发布?那是有道理的。 我想您已经发出了获取包含该内容的可读数据“jsonData”的请求。由于您表明您还没有 json 数据,因此这可能会起作用。

var url = NSMutableURLRequest(URL: NSURL(string: "http://api.football-data.org/v1/soccerseasons/424/leagueTable")!)
url.addValue("mytokenishere", forHTTPHeaderField: "X-Auth-Token")
url.HTTPMethod = "GET"
NSURLSession.sharedSession().dataTaskWithRequest(url, completionHandler: data, response, error in {
    if error == nil {
        var readableJSON = JSON(data: data, options: .MutableContainers, error: nil)
        let name = readableJSON["standings"]
        url.HTTPBody = try! name.rawData()
        NSURLSession.sharedSession().dataTaskWithRequest(url, completionHandler: data, response, error in {
           //do something with his response from getting the data
        })
    } else {
        print(error)
    }
})

这是一种很奇怪的方法,但我认为这就是你想要的

关于json - SwiftyJSON : How can I add token?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37980746/

相关文章:

json - 在 Http Header 中使用 Json 字符串

c - 如何在 C Linux 中解析 JSON 响应?

ios - 如何在 ios swift 中显示来自 url 的图像

swift - viewForAnnotation 未被调用(使用 Alamofire 解析 JSON)

json - 新手Go,试图梳理如何处理JSON

json - 如何编码和转义引号

swift - 如何通过 FacebookUtil 链接现有的 Parse 用户

ios - 如何为自定义表格 View 单元动态设置约束?

arrays - swift 3 : binary operator cannot be applied to operands of type int and 'Int?'

ios - 通过解析JSON创建Class对象数组- SwiftyJSON