目标:仅获取选定加密货币的价格值(value)!使用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()][2]
或 [.split()][3] + [.join()][ 4]
仅获取价格,但我确实相信有比使用该方法更简单的方法。
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()
我的问题是,如何才能仅以文本形式获取所选加密货币的价格值?
最佳答案
我认为你可以这样做
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
var strobj = JSON.parse(str)
document.body.innerHTML = strobj.price
}
ourRequest.send()
关于javascript - 我怎样才能只获取所选加密货币的价格值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67528357/