我无法将 OHCLV 数据从 JSON 解析为List<Candle>
.
Newtonsoft.Json.JsonSerializationException:'无法将当前 JSON 数组(例如 [1,2,3])反序列化为类型 'Models.Candle',因为该类型需要 JSON 对象(例如 {"name":"value"})正确反序列化。要修复此错误,请将 JSON 更改为 JSON 对象(例如 {"name":"value"})或将反序列化类型更改为数组或实现集合接口的类型(例如 ICollection、IList),例如可以从 JSON 数组反序列化。JsonArrayAttribute 也可以添加到类型中以强制它从 JSON 数组反序列化。路径“[0]”,第 1 行,位置 2。
它只允许我使用List<List<double>>
. 我怎样才能做到这一点?
[[1604666100000,0.02585,0.02585,0.02577,0.02577,2346260.5],[1604666400000,0.02577,0.02577,0.02571,0.02572,3853038.7000000002],[1604666700000,0.02572,0.02573,0.02568,0.02573,2525735.5],[1604667000000,0.02573,0.02578,0.02573,0.02574,2519284.3999999999],[1604667300000,0.02575,0.02582,0.02574,0.02578,1463562.6000000001],[1604667600000,0.02578,0.02587,0.02577,0.02585,2074134.3]]
代码:
public class Candle
{
public DateTime OpenTime { get; set; }
public decimal High { get; set; }
public decimal Low { get; set; }
public decimal Open { get; set; }
public decimal Close { get; set; }
public decimal Volume { get; set; }
}
public static List<Candle> LoadCandles(string path)
{
var basePath = AppDomain.CurrentDomain.BaseDirectory;
var filePath = Path.Combine(basePath, path);
if (!File.Exists(filePath))
throw new FileNotFoundException($"The .json '{filePath}' file used to load the candles from was not found.");
var data = File.ReadAllText(filePath);
//var candles3 = JsonConvert.DeserializeObject<List<List<double>>>(data); // this one works
var candles = JsonConvert.DeserializeObject<List<Candle>>(data);
return candles;
}