ColdFusion:从内部结构获取数组 - Binance API 结果

发布于 2025-01-09 09:48:24 字数 1425 浏览 1 评论 0原文

Horray - 让 ColdFusion 到 Binance API 正常工作。

Binance API 结果在 JSON 结构中有一个数组。

尝试提取填充数据。但运气并不好。

 <cfset result = '{"symbol":"SHIBUSDT","orderId":1158141049,"orderListId":-1,"clientOrderId":"tAMnbc6XLKEMNFENUdbNvD","transactTime":1645634941287,"price":"0.00000000","origQty":"784913.00","executedQty":"784913.00","cummulativeQuoteQty":"20.72170320","status":"FILLED","timeInForce":"GTC","type":"MARKET","side":"SELL","fills":[{"price":"0.00002640","qty":"784913.00","commission":"0.02072170","commissionAsset":"USDT","tradeId":291444301}]}'>

我需要的数组从填充开始,其内部结构如下:

  ,"fills":[{"price":"0.00002640","qty":"784913.00","commission":"0.02072170","commissionAsset":"USDT","tradeId":291444301}]

从这里开始,轻松显示 JSON 结果,轻松获取符号、侧面等...

 <cfscript>
   record = deserializeJSON(#result#);
   writeOutput( "<br>SYMBOL:"& record.symbol); 
   writeOutput( "<br>SIDE:"& record.side);   
 </cfscript>

有点尝试通过此方法获取填充数组数据,但没有运气。 。

 <cfoutput>
 <cfset arrayOfStructs = deserializeJson(result[fills])>
 <cfloop array="#arrayOfStructs#" index="retdata">
       <cfset Price = retdata.price />
       <cfset QTY = retdata.qty />
       #price#
          <br>#qty#
 </cfloop>
 </cfoutput>

应该是一件容易的事 也许我的大脑因我所经历的所有 API 之争而被烧伤了。

Horray - got the ColdFusion to Binance API working.

Binance API Result has an Array Inside the JSON Structure.

Trying to pull the Fills data. But not having best luck.

 <cfset result = '{"symbol":"SHIBUSDT","orderId":1158141049,"orderListId":-1,"clientOrderId":"tAMnbc6XLKEMNFENUdbNvD","transactTime":1645634941287,"price":"0.00000000","origQty":"784913.00","executedQty":"784913.00","cummulativeQuoteQty":"20.72170320","status":"FILLED","timeInForce":"GTC","type":"MARKET","side":"SELL","fills":[{"price":"0.00002640","qty":"784913.00","commission":"0.02072170","commissionAsset":"USDT","tradeId":291444301}]}'>

The array I need starts at the fills with the structure inside it:

  ,"fills":[{"price":"0.00002640","qty":"784913.00","commission":"0.02072170","commissionAsset":"USDT","tradeId":291444301}]

Starting with this as easy display of the JSON result to easily get the symbol, side, etc...

 <cfscript>
   record = deserializeJSON(#result#);
   writeOutput( "<br>SYMBOL:"& record.symbol); 
   writeOutput( "<br>SIDE:"& record.side);   
 </cfscript>

Kinda trying this to get the Fills Array Data but no luck...

 <cfoutput>
 <cfset arrayOfStructs = deserializeJson(result[fills])>
 <cfloop array="#arrayOfStructs#" index="retdata">
       <cfset Price = retdata.price />
       <cfset QTY = retdata.qty />
       #price#
          <br>#qty#
 </cfloop>
 </cfoutput>

Should be an easy thing. Perhaps my brain is burned from all the API fights I've had.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

三生池水覆流年 2025-01-16 09:48:24

我能够解决。感谢您的回答。我也会看看该代码。

简单的 CFHTTP POST 到 Binance API - 用于交易(买入/卖出)

 <cfhttp url="#base_api##req_path#" method="POST" result="result" charset="utf-8">
 <cfhttpparam type="header" name="X-MBX-APIKEY" value="#bn_key#"> 
 <cfhttpparam type="body" value="#thebody#">
 </cfhttp> 

 <cfset result = #result.filecontent#>

 <cfscript>
   record = deserializeJSON(#result#);

   need = record.fills;

   writeOutput("SYMBOL:"& record.symbol); 
   writeOutput("<br>SIDE:"& record.side);  

   // writeDump(need);
   // writeDump(record);

 </cfscript>

这会获取所需的数组 [need],其中包含实际的买入/卖出数据。然后循环播放即可。

 <cfoutput>
 <cfloop array="#need#" index="retdata">
       <cfset Price = retdata.price />
       <cfset QTY = retdata.qty />
      PRICE : #price#
    <br>Quantity : #qty#
 </cfloop>
 </cfoutput>

I was able to sort out. Thx for answers. I'll look at that code as well.

Simple CFHTTP POST to the Binance API - for Trades (Buy/Sell)

 <cfhttp url="#base_api##req_path#" method="POST" result="result" charset="utf-8">
 <cfhttpparam type="header" name="X-MBX-APIKEY" value="#bn_key#"> 
 <cfhttpparam type="body" value="#thebody#">
 </cfhttp> 

 <cfset result = #result.filecontent#>

 <cfscript>
   record = deserializeJSON(#result#);

   need = record.fills;

   writeOutput("SYMBOL:"& record.symbol); 
   writeOutput("<br>SIDE:"& record.side);  

   // writeDump(need);
   // writeDump(record);

 </cfscript>

This gets the needed array [need] which has the actual buy/sell data. Then just loop it.

 <cfoutput>
 <cfloop array="#need#" index="retdata">
       <cfset Price = retdata.price />
       <cfset QTY = retdata.qty />
      PRICE : #price#
    <br>Quantity : #qty#
 </cfloop>
 </cfoutput>
春花秋月 2025-01-16 09:48:24

record = deserializeJSON(#result#);
..

您非常接近,但无需反序列化两次。只需使用现有的 record 变量即可。由于 result[fills] 已经是一个 CF 数组,只需循环遍历它:

<cfscript>
  result = '{"symbol":"SHIBUSDT",....}'; 

  record = deserializeJSON(result);
  writeOutput( "<br>SYMBOL:"& record.symbol); 
  writeOutput( "<br>SIDE:"& record.side);     

  for (fill in record.fills) {
    price = fill.price;
    qty = fill.qty;
    
    writeOutput("<br>price="& price &" qty="& qty);
  }
  
</cfscript>

更新 2/29/2022:有关错误处理的注意事项

为了简洁起见,您可能省略了 cfhttp 错误处理,但如果没有......在尝试对响应执行任何操作之前,验证 http 调用实际上是否成功(并且它包含预期的 JSON 字符串)始终是一个好习惯。否则,如果 Web 服务遇到问题,代码可能会崩溃并出现神秘错误。把它们放在一起......

<cfscript>
  // Using all cfscript for consistency   
  cfhttp(url="#base_api##req_path#" 
         , method="POST" 
         , result="result" 
         , charset="utf-8") {
     cfhttpparam( type="header", name="X-MBX-APIKEY", value="#bn_key#");
     cfhttpparam( type="body", value=theBody );
  }
  
  if (result.statusCode eq "200 OK" && IsJSON( result.fileContent )) {
     
     response = deserializeJSON( result.fileContent );
     writeOutput( "<br>SYMBOL:"& response.symbol); 
     writeOutput( "<br>SIDE:"& response.side);     

     for (fill in response.fills) {
        price = fill.price;
        qty = fill.qty;
    
        writeOutput("<br>price="& price &" qty="& qty);
     }
    
  
  }
  else {
     WriteOutput("ERROR: Houston we have a problem...");  
     writeDump( result );
  }
</cfscript>

record = deserializeJSON(#result#);
..
<cfset arrayOfStructs = deserializeJson(result[fills])>

You're very close, but no need to deserialize twice. Just use the existing record variable. Since result[fills] is already a CF array, just loop through it:

<cfscript>
  result = '{"symbol":"SHIBUSDT",....}'; 

  record = deserializeJSON(result);
  writeOutput( "<br>SYMBOL:"& record.symbol); 
  writeOutput( "<br>SIDE:"& record.side);     

  for (fill in record.fills) {
    price = fill.price;
    qty = fill.qty;
    
    writeOutput("<br>price="& price &" qty="& qty);
  }
  
</cfscript>

Update 2/29/2022: Note about Error Handling

You may have omitted cfhttp error handling for brevity, but if not ... it's always a good practice to verify the http call was actually successful (and that it contains the expected JSON string) before attempting to do anything with the response. Otherwise, the code may crash with an cryptic error if the web service is experiencing problems. Putting it all together ...

<cfscript>
  // Using all cfscript for consistency   
  cfhttp(url="#base_api##req_path#" 
         , method="POST" 
         , result="result" 
         , charset="utf-8") {
     cfhttpparam( type="header", name="X-MBX-APIKEY", value="#bn_key#");
     cfhttpparam( type="body", value=theBody );
  }
  
  if (result.statusCode eq "200 OK" && IsJSON( result.fileContent )) {
     
     response = deserializeJSON( result.fileContent );
     writeOutput( "<br>SYMBOL:"& response.symbol); 
     writeOutput( "<br>SIDE:"& response.side);     

     for (fill in response.fills) {
        price = fill.price;
        qty = fill.qty;
    
        writeOutput("<br>price="& price &" qty="& qty);
     }
    
  
  }
  else {
     WriteOutput("ERROR: Houston we have a problem...");  
     writeDump( result );
  }
</cfscript>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文