I'm unable to parse OHCLV data from a JSON into List<Candle>
.
Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Models.Candle' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path '[0]', line 1, position 2.'
It only allows me to do it with List<List<double>>
. How can I achieve that?
[[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]]
Code:
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;
}