切换到 websocket 后如何从 Binance 解析数据

how to parse data from Binance after switching to websocket

现在我终于在 Binance 中切换到 websocket,我无法再像以前使用旧脚本那样解析响应中的数据。我正在使用 Javascript 和 Google Apps Script

function balanceBinance() {
  
  var key = '';
  var secret = '';

  var curTime = Number(new Date().getTime()).toFixed(0);
  var string = "type=SPOT&timestamp=" + curTime;
  var sKey = Utilities.computeHmacSha256Signature(string, secret);
  sKey = sKey.map(function(e) {
      var v = (e < 0 ? e + 256 : e).toString(16);
      return v.length == 1 ? "0" + v : v;
  }).join("");
  var params = {
    'type':'SPOT',
    'method': 'GET',
    'headers': {'X-MBX-APIKEY': key},
    'muteHttpExceptions': true
  };

  var url = "https://api.binance.com/sapi/v1/accountSnapshot?" + string + "&signature=" + sKey;
  var data = UrlFetchApp.fetch(url, params);
  var data = JSON.parse(data.getContentText());  
  var data = data.snapshotVos; 

Logger.log(data);
}

它returns:

[{data={totalAssetOfBtc=0.0000117, balances=[{free=0.0000117, asset=BNB, locked=0}, {asset=BTC, locked=0, free=0.00000093}, {free=0.00026, asset=BTCDOWN, locked=0}, {locked=0, free=0.00, asset=EUR}, {asset=USDT, locked=0, free=0.06716916}]}, type=spot, updateTime=1.622851199E12}, {updateTime=1.622937599E12, data={totalAssetOfBtc=0.0000117, balances=[{asset=BNB, locked=0, free=0.0000117}, {locked=0, asset=BTC, free=0.00000093}, {locked=0, free=0.00026, asset=BTCDOWN}, {locked=0, free=0.34585555, asset=EUR}, {free=0.06716916, locked=0, asset=USDT}]}, type=spot}, {type=spot, updateTime=1.623023999E12, data={balances=[{locked=0, asset=BNB, free=0.0000117}, {asset=BTC, locked=0, free=0.00000093}, {asset=BTCDOWN, locked=0, free=0.00026}, {free=0.34585555, locked=0, asset=EUR}, {free=411.06716916, locked=0, asset=USDT}], totalAssetOfBtc=0.11298258}}, {updateTime=1.623110399E12, type=spot, data={balances=[{free=0.0000117, locked=0, asset=BNB}, {asset=BTC, locked=0, free=0.00000093}, {locked=0, asset=BTCDOWN, free=0.00026}, {asset=ETHDOWN, free=0.00376, locked=0}, {free=0.34585555, asset=EUR, locked=0}, {free=0.40578148, locked=0, asset=USDT}], totalAssetOfBtc=0.0000117}}, {data={totalAssetOfBtc=0.0000117, balances=[{free=0.0000117, locked=0, asset=BNB}, {free=0.00000093, asset=BTC, locked=0}, {free=0.00026, asset=BTCDOWN, locked=0}, {free=0.00376, asset=ETHDOWN, locked=0}, {free=0.34585555, asset=EUR, locked=0}, {free=0.40578148, locked=0, asset=USDT}]}, type=spot, updateTime=1.623196799E12}]

在以前版本的脚本中,我能够使用以下代码为任何相应的 asset 提取 free 参数:

var data = data.filter(function (el) {
     return el.free > 0; 
});
  
  var i;
  for (i=0; i < data.length; i++) {
 
  var asset = data[i].asset;
  var free  = data[i].free; 
    if (free  < 1)                {var a = parseFloat(free);   var a = a.toFixed(8);   var free = parseFloat(a);} 
    if (free  > 1 && free < 10)   {var a = parseFloat(free);   var a = a.toFixed(4);   var free = parseFloat(a);}
    if (free  >=10)               {var a = parseFloat(free);   var a = a.toFixed(2);   var free = parseFloat(a);}
    
  var row=rows.indexOf(asset)+2;   
     if (row>0){
       sheet.getRange(row, 13).setValue(free);}    
  }

你的JSON结构是这样的

snapshotVos = [
  { data: { balances: [{asset,free}, {asset,free}] } },
  { data: { balances: [{asset,free}, {asset,free}] } },
  { data: { balances: [{asset,free}, {asset,free}] } } ]

示例代码:

function parseJson() {
  var sampleJson ={msg:'', snapshotVos:[{updateTime:1.622851199E12, data:{totalAssetOfBtc:0.10958809, balances:[{asset:'BNB', free:0.0000, locked:0}, {asset:'BTC', locked:0, free:0.00000093}, {asset:'BTCDOWN', free:0.00026, locked:0}, {free:2981.34585555, asset:'EUR', locked:0}, {free:411.06716916, asset:'USDT', locked:0}]}, type:'spot'}, {data:{balances:[{asset:'BNB', locked:0, free:0.0000117}, {asset:'BTC', locked:0, free:0.00000093}, {asset:'BTCDOWN', locked:0, free:0.00026}, {locked:0, free:2981.34585555, asset:'EUR'}, {asset:'USDT', free:411.06716916, locked:0}], totalAssetOfBtc:0.11403012}, updateTime:1.622937599E12, type:'spot'}], code:200.0}; 

  var sampleJsonStr = JSON.stringify(sampleJson);
  var data = JSON.parse(sampleJsonStr);
  var balances = []

  data.snapshotVos.forEach( snap => {
    balances.push(snap.data.balances);
  });
  Logger.log(balances);
  Logger.log(balances.flat());
  
  var data = balances.flat().filter(function (el) {
      return el.free > 0; 
  });

  Logger.log(data);
  for (i=0; i < data.length; i++) {
 
    var asset = data[i].asset;
    var free  = data[i].free; 
    Logger.log("asset: "+asset+" "+"free: "+free);
  }

}

它有什么作用?

  1. 我根据您的示例数据创建了一个 JSON 对象,使用 JSON.stringify() 将其转换为字符串,然后使用 JSON.parse( )
  2. 我将 snapshotVos array 中的所有 data.balances array 合并为一个 balances array。我使用 array.flat() 将二维数组更改为一维数组,然后根据您给定的条件对其进行过滤。
  3. data 变量应该已经与您的 for 循环相匹配。