這個問題在這里已經有了答案: 如何訪問和處理嵌套物件、陣列或 JSON? (30 個回答) 6 小時前關閉。
所以我正在使用 Binance API 并且我在控制臺中獲得了我需要的必要資訊,但我想在我的網站上為我的客戶創建一個 BTC 到 USDT 轉換計算器,以便他們可以檢查,但我可以' t 弄清楚如何將價格的變化值轉化為變數。對不起,如果這是一個愚蠢的問題,但我是 JavaScript 和 API 的新手。
這是我的代碼:
<!DOCTYPE html>
<html>
<body>
<div id="log">
<h4><b>BTC:</b></h4>
<h4><b>USD:</b></h4>
</div>
</body>
<script>
var baseurl = "https://api.binance.com";
var call = '/api/v3/ticker/price';
call = '?symbol=BTCUSDT';
var url = baseurl call;
var request = new XMLHttpRequest();
request.open('GET',url,true);
request.onload = function(){
console.log(request.responseText);
}
request.send();
</script>
</html>
uj5u.com熱心網友回復:
修改了你的代碼
<!DOCTYPE html>
<html>
<body>
<div id="log">
<h4 class="btc"><b>BTC:</b></h4>
<h4 class="usd"><b>USD:</b></h4>
</div>
</body>
<script>
var baseurl = "https://api.binance.com";
var call = '/api/v3/ticker/price';
call = '?symbol=BTCUSDT';
var url = baseurl call;
var request = new XMLHttpRequest();
request.open('GET',url,true);
request.onload = function(){
console.log(request.responseText);
var data = JSON.parse(request.responseText);
console.log(data);
var rate = 10; //You need some sort of a conversion rate here. I guess
var btc = data.price; //We get the price
var symbol = data.symbol; //We get the symbol
var usd = btc * rate; //We convert the price to USD.
var log = document.querySelector('#log'); //We get the log div which is the parent of the USD and BTC elements
//We replace the content of the div with the new data.
log.innerHTML = '<h4 ><b>BTC:</b> ' btc '</h4><h4 ><b>USD:</b> ' usd '</h4>';
//Or we could do it this way:
log.querySelector('.btc').innerHTML = 'BTC: ' btc " " symbol;
log.querySelector('.usd').innerHTML = 'USD: ' usd;
}
request.send();
</script>
</html>
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/359497.html
標籤:javascript html 接口 币安 币安-api-客户端
下一篇:從多個陣列狀態中洗掉索引元素
