我正在使用 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/