简体   繁体   English

从 Binance API 获取数据到 Javascript 数组

[英]Fetch data from Binance API to Javascript Array

I just started to work with API's and got a little bit confused of how can I transfer the data which I receive to a JavaScript array.我刚开始使用 API,对如何将收到的数据传输到 JavaScript 阵列有点困惑。 I have this code which receive the data from Binance API and show it in console.我有这个代码从 Binance API 接收数据并在控制台中显示。

var burl ='https://api.binance.com';

var query ='/api/v3/klines';

query += '?symbol=BTCUSDT&interval=15m&limit=2';

var url = burl + query;

var ourRequest = new XMLHttpRequest();

ourRequest.open('GET',url,true);
ourRequest.onload = function(){
console.log(ourRequest.responseText);
}
ourRequest.send();

I also have a hard-scripted chart from FusionCharts Library.我还有一个来自 FusionCharts Library 的硬脚本图表。 The source code of a chart is here - FusionChart Candlestick Chart图表的源代码在这里 - FusionChart Candlestick Chart

const dataSource = {
chart: {
caption: "Bitcoin Price",
subcaption: "Q4-2017",
numberprefix: "$",
pyaxisname: "Price (USD)",
showvolumechart: "1",
vnumberprefix: "$",
vyaxisname: "Volume traded",
exportenabled: 1,
theme: loadedTheme || ThemeAliases.light
 },
categories: [
{
  category: [
    {
      label: "Jan",
      x: "1"
    },
    {
      label: "Feb",
      x: "32"
    },
    {
      label: "Mar",
      x: "62"
    },
    {
      label:"Apr",
      x:"12"
    }
  ]
}
],
dataset: [
{
  data: [
    {
      tooltext:
        "<b>Oct 01, 2017</b><br>Open: <b>$openDataValue</b><br>Close: <b>$closeDataValue</b><br>High: <b>$highDataValue</b><br>Low: <b>$lowDataValue</b><br>Volume: <b>$volumeDataValue</b>",
      open: 4341.05,
      high: 4403.74,
      low: 4269.81,
      close: 4403.74,
      volume: 1208210000,
      x: 1
    },
FusionCharts.ready(function() {
var myChart = new FusionCharts({
type: "candlestick",
renderAt: "chart-container",
width: "75%",
height: "100%",
dataFormat: "json",
dataSource

}).render();
});

The result ourRequest.responseText is returned as a string, and not as an array.结果ourRequest.responseText作为字符串而不是数组返回。 To fix it, simply use the JSON.parse method.要修复它,只需使用JSON.parse方法。 You also store it in a variable, example:您还将它存储在一个变量中,例如:

 var burl ='https://api.binance.com'; var query ='/api/v3/klines'; query += '?symbol=BTCUSDT&interval=15m&limit=2'; var url = burl + query; var ourRequest = new XMLHttpRequest(); ourRequest.open('GET',url,true); ourRequest.onload = function(){ // Will convert the string to something Javascript can understand var result = JSON.parse(ourRequest.responseText); // You can now use it as an array console.log(result); } ourRequest.send();

Does that answer the question?这能回答问题吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM