【发布时间】:2021-08-04 06:37:09
【问题描述】:
目标:只获取选定加密货币的价格价值!使用Binance Public API
问题:返回整个 JSON 字符串,而不仅仅是所选货币的价格。
代码:
var burl = 'https://api.binance.com/api/v3/ticker/price?symbol='
var symbol = 'BTCUSDT'
var url = burl + symbol
var ourRequest = new XMLHttpRequest()
ourRequest.open('GET', url, true)
ourRequest.onload = function() {
console.log(ourRequest.responseText)
}
ourRequest.send()
现在说句公道话,我知道我可以使用.replace() 或.split() + .join() 来获取价格,但我确实相信有比使用这种方法更简单的方法。
var burl = 'https://api.binance.com/api/v3/ticker/price?symbol='
var symbol = 'BTCUSDT'
var url = burl + symbol
var ourRequest = new XMLHttpRequest()
ourRequest.open('GET', url, true)
ourRequest.onload = function() {
var str = ourRequest.responseText
str = str.split('{"symbol":"BTCUSDT","price":"').join('')
str = str.split('"}').join('')
document.body.innerHTML = str
}
ourRequest.send()
我的问题是,我怎样才能只获取所选加密货币的价格值作为文本?
【问题讨论】:
标签: javascript html json api binance