Closed Polotenec closed 2 years ago
You must first deserialise the json response using System.Text.Json
or Newtonsoft.Json
.
Here is an example using System.Text.Json
namespace Binance.Spot.SpotAccountTradeExamples
{
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Binance.Common;
using Binance.Spot;
using Binance.Spot.Models;
using Microsoft.Extensions.Logging;
using System.Text.Json.Nodes; // Import package
public class QueryOrder_Example
{
public static async Task Main(string[] args)
{
using var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddConsole();
});
ILogger logger = loggerFactory.CreateLogger<QueryOrder_Example>();
HttpMessageHandler loggingHandler = new BinanceLoggingHandler(logger: logger);
HttpClient httpClient = new HttpClient(handler: loggingHandler);
string apiKey = "api-key";
string apiSecret = "api-secret";
string symbol = "BTCUSDT";
string orderId = 123;
var spotAccountTrade = new SpotAccountTrade(httpClient, apiKey: apiKey, apiSecret: apiSecret);
var result = await spotAccountTrade.QueryOrder(symbol, orderId);
JsonNode orderResult = JsonNode.Parse(result); // Parse json string
Console.WriteLine(orderResult["orderId"]); // Output deserialized object's property
}
}
}
Сначала вы должны десериализовать ответ json, используя
System.Text.Json
илиNewtonsoft.Json
.Вот пример использования
System.Text.Json
namespace Binance.Spot.SpotAccountTradeExamples { using System; using System.Net; using System.Net.Http; using System.Threading.Tasks; using Binance.Common; using Binance.Spot; using Binance.Spot.Models; using Microsoft.Extensions.Logging; using System.Text.Json.Nodes; // Import package public class QueryOrder_Example { public static async Task Main(string[] args) { using var loggerFactory = LoggerFactory.Create(builder => { builder.AddConsole(); }); ILogger logger = loggerFactory.CreateLogger<QueryOrder_Example>(); HttpMessageHandler loggingHandler = new BinanceLoggingHandler(logger: logger); HttpClient httpClient = new HttpClient(handler: loggingHandler); string apiKey = "api-key"; string apiSecret = "api-secret"; string symbol = "BTCUSDT"; string orderId = 123; var spotAccountTrade = new SpotAccountTrade(httpClient, apiKey: apiKey, apiSecret: apiSecret); var result = await spotAccountTrade.QueryOrder(symbol, orderId); JsonNode orderResult = JsonNode.Parse(result); // Parse json string Console.WriteLine(orderResult["orderId"]); // Output deserialized object's property } } }
Thanks a lot! Everything worked out, my code was reduced at times.
Why doesn't the client deserialize the response itself?
Description