Swift:尝试查询时得到 nil api
Swift: Getting nil when trying to query api
我无法从 api 获取股票价格。我正在使用 SwiftyJSON 框架来接收数据。当我执行代码时,它在控制台中给我 nil 。我不知何故找不到错误。
import UIKit
class ViewController: UIViewController
{
@IBOutlet weak var inputQuote: UITextField!
@IBAction func buttonTapped(sender: AnyObject)
{
var stockQuote = inputQuote.text
getJSON(stockQuote!)
}
override func viewDidLoad()
{
super.viewDidLoad()
}
func getJSON(quote: String)
{
let markitOnDemandURL = "http://dev.markitondemand.com/MODApis/Api/v2/Quote/jsonp?symbol=" + quote.uppercaseString + "&callback=myFunction"
let url = NSURL(string: markitOnDemandURL)
let request = NSURLRequest(URL: url!)
let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())
let task = session.dataTaskWithRequest(request) {(data, response, error) -> Void in
if error == nil {
let swiftyJSON = JSON(data: data!)
let stockPrice = swiftyJSON["LastPrice"].stringValue
print(stockPrice)
}
else
{
print("error")
}
}
task.resume()
}
}
这是我正在使用的 api:http://dev.markitondemand.com/MODApis/Api/v2/Quote/jsonp?symbol=AAPL&callback=myFunction
如果你能看看我的代码并帮助我解决这个问题,那就太好了。感谢您的努力。
您目前从这个 api 得到的结果是
myFunction({"Status":"SUCCESS","Name":"Apple Inc","Symbol":"AAPL","LastPrice":116.05,"Change":1.98999999999999,"ChangePercent":1.74469577415395,"Timestamp":"Mon Oct 10 00:00:00 UTC-04:00 2016","MSDate":42653,"MarketCap":625328810150,"Volume":36235956,"ChangeYTD":105.26,"ChangePercentYTD":10.2508075242257,"High":116.75,"Low":114.72,"Open":115.02})
这是一个 无效 JSON
从开头删除 myFunction( 并在末尾删除 ) 将使它成为有效的 json
即
{
"Status": "SUCCESS",
"Name": "Apple Inc",
"Symbol": "AAPL",
"LastPrice": 116.05,
"Change": 1.98999999999999,
"ChangePercent": 1.74469577415395,
"Timestamp": "Mon Oct 10 00:00:00 UTC-04:00 2016",
"MSDate": 42653,
"MarketCap": 625328810150,
"Volume": 36235956,
"ChangeYTD": 105.26,
"ChangePercentYTD": 10.2508075242257,
"High": 116.75,
"Low": 114.72,
"Open": 115.02
}
您可以使用 POSTMAN 等各种工具或 http://jsonviewer.stack.hu/ and http://jsonlint.com/ 等链接来验证您的 JSON。
我无法从 api 获取股票价格。我正在使用 SwiftyJSON 框架来接收数据。当我执行代码时,它在控制台中给我 nil 。我不知何故找不到错误。
import UIKit
class ViewController: UIViewController
{
@IBOutlet weak var inputQuote: UITextField!
@IBAction func buttonTapped(sender: AnyObject)
{
var stockQuote = inputQuote.text
getJSON(stockQuote!)
}
override func viewDidLoad()
{
super.viewDidLoad()
}
func getJSON(quote: String)
{
let markitOnDemandURL = "http://dev.markitondemand.com/MODApis/Api/v2/Quote/jsonp?symbol=" + quote.uppercaseString + "&callback=myFunction"
let url = NSURL(string: markitOnDemandURL)
let request = NSURLRequest(URL: url!)
let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())
let task = session.dataTaskWithRequest(request) {(data, response, error) -> Void in
if error == nil {
let swiftyJSON = JSON(data: data!)
let stockPrice = swiftyJSON["LastPrice"].stringValue
print(stockPrice)
}
else
{
print("error")
}
}
task.resume()
}
}
这是我正在使用的 api:http://dev.markitondemand.com/MODApis/Api/v2/Quote/jsonp?symbol=AAPL&callback=myFunction
如果你能看看我的代码并帮助我解决这个问题,那就太好了。感谢您的努力。
您目前从这个 api 得到的结果是
myFunction({"Status":"SUCCESS","Name":"Apple Inc","Symbol":"AAPL","LastPrice":116.05,"Change":1.98999999999999,"ChangePercent":1.74469577415395,"Timestamp":"Mon Oct 10 00:00:00 UTC-04:00 2016","MSDate":42653,"MarketCap":625328810150,"Volume":36235956,"ChangeYTD":105.26,"ChangePercentYTD":10.2508075242257,"High":116.75,"Low":114.72,"Open":115.02})
这是一个 无效 JSON
从开头删除 myFunction( 并在末尾删除 ) 将使它成为有效的 json
即
{
"Status": "SUCCESS",
"Name": "Apple Inc",
"Symbol": "AAPL",
"LastPrice": 116.05,
"Change": 1.98999999999999,
"ChangePercent": 1.74469577415395,
"Timestamp": "Mon Oct 10 00:00:00 UTC-04:00 2016",
"MSDate": 42653,
"MarketCap": 625328810150,
"Volume": 36235956,
"ChangeYTD": 105.26,
"ChangePercentYTD": 10.2508075242257,
"High": 116.75,
"Low": 114.72,
"Open": 115.02
}
您可以使用 POSTMAN 等各种工具或 http://jsonviewer.stack.hu/ and http://jsonlint.com/ 等链接来验证您的 JSON。