作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 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/
我是一名优秀的程序员,十分优秀!