如果 candles.json 不存在或存在,并且第一个元素的开始日期和最后一个元素的结束日期不同,它将把数据序列化为List<IBinanceKline>
并创建 candles.json 。如果 candles.json 存在,则应读取它并将数据反序列化为List<IBinanceKline>
。目前,它无法DeserializeObject<List<IBinanceKline>>
并显示以下错误消息:
Newtonsoft.Json.JsonSerializationException:'无法将当前JSON数组(例如[1,2,3])反序列化为类型'Binance.Net.Interfaces.IBinanceKline',因为该类型需要JSON对象(例如{“ name” :“ value”})正确反序列化。 要解决此错误,可以将JSON更改为JSON对象(例如{“ name”:“ value”}),也可以将反序列化类型更改为数组,或者将实现集合接口的类型(例如ICollection,IList)更改为List,例如List从JSON数组反序列化。还可以将JsonArrayAttribute添加到类型中,以强制其从JSON数组反序列化。 路径'[0]',第1行,位置2。'
有趣的是,代码在更新库之前曾经可以工作,现在使用IBinanceKline代替BinanceKline。
candles.json看起来像这样:
[[1598918400000,0.02916000,0.02933000,0.02805000,0.02840000,160360176.00000000,1598920199999,4581586.82491700,8467,65052915.90000000,1858526.79404600,0.0],[1598920200000,0.02841000,0.02875000,0.02832000,0.02867000,58358821.40000000,1598921999999,1666101.98325900,3117,22117012.60000000,631314.89648100,0.0]]
List<IBinanceKline> candlesFromFile = null;
if (File.Exists(CandlesFile))
{
var content = File.ReadAllText(CandlesFile);
candlesFromFile = JsonConvert.DeserializeObject<List<IBinanceKline>>(content); // Exception thrown here
}
List<IBinanceKline> candles;
if (candlesFromFile != null && candlesFromFile.First().OpenTime == StartDate && candlesFromFile.Last().OpenTime == EndDate)
{
candles = candlesFromFile;
}
else
{
candles = GetCandlesByStartDateEndDate(symbol, interval, StartDate, EndDate);
string serializedObject = JsonConvert.SerializeObject(candles);
File.WriteAllText(CandlesFile, serializedObject);
}
private List<IBinanceKline> GetCandlesByStartDateEndDate(string symbol, KlineInterval interval, DateTime startDate, DateTime endDate)
{
if (endDate.Date > DateTime.UtcNow.Date)
throw new ArgumentOutOfRangeException("The end date is greater than today.");
var candles = _client.Spot.Market.GetKlines(symbol, interval, startTime: startDate, endTime: endDate, limit: 200).Data.ToList();
while (endDate > candles.Last().OpenTime)
{
var moreCandles = _client.Spot.Market.GetKlines(symbol, interval, startTime: candles.Last().OpenTime, endTime: endDate, limit: 1000).Data.ToList();
candles.AddRange(moreCandles.ExceptUsingJsonCompare(candles));
}
return candles;
}
public interface IBinanceKline
{
DateTime OpenTime { get; set; }
decimal Open { get; set; }
decimal High { get; set; }
decimal Low { get; set; }
decimal Close { get; set; }
decimal BaseVolume { get; set; }
DateTime CloseTime { get; set; }
decimal QuoteVolume { get; set; }
int TradeCount { get; set; }
decimal TakerBuyBaseVolume { get; set; }
decimal TakerBuyQuoteVolume { get; set; }
}
答案 0 :(得分:0)
感谢您的评论,之所以出现此问题,是因为List<IBinanceKline>
并未真正指定将序列化到的类。实际上,它确实序列化为List<BinanceSpotKline>
。
解决方案:
List<BinanceSpotKline> candlesFromFile = null;
if (File.Exists(CandlesFile))
{
var content = File.ReadAllText(CandlesFile);
candlesFromFile = JsonConvert.DeserializeObject<List<BinanceSpotKline>>(content);
}
List<BinanceSpotKline> candles;
if (candlesFromFile != null && candlesFromFile.First().OpenTime == StartDate && candlesFromFile.Last().OpenTime == EndDate)
{
candles = candlesFromFile;
}
else
{
candles = GetCandlesByStartDateEndDate(symbol, interval, StartDate, EndDate);
string serializedObject = JsonConvert.SerializeObject(candles);
File.WriteAllText(CandlesFile, serializedObject);
}
private List<BinanceSpotKline> GetCandlesByStartDateEndDate(string symbol, KlineInterval interval, DateTime startDate, DateTime endDate)
{
if (endDate.Date > DateTime.UtcNow.Date)
throw new ArgumentOutOfRangeException("The end date is greater than today.");
var candles = _client.Spot.Market.GetKlines(symbol, interval, startTime: startDate, endTime: endDate, limit: 200).Data.ToList();
while (endDate > candles.Last().OpenTime)
{
var moreCandles = _client.Spot.Market.GetKlines(symbol, interval, startTime: candles.Last().OpenTime, endTime: endDate, limit: 1000).Data.ToList();
candles.AddRange(moreCandles.ExceptUsingJsonCompare(candles));
}
return candles.Cast<BinanceSpotKline>().ToList();
}