JSON의 OHCLV 데이터를 List<Candle>
.
Newtonsoft.Json.JsonSerializationException : '현재 JSON 배열 (예 : [1,2,3])을'Models.Candle '유형으로 역 직렬화 할 수 없습니다. 유형에 JSON 개체 (예 : { "name": "value"})가 필요하기 때문에 올바르게 역 직렬화하십시오. 이 오류를 수정하려면 JSON을 JSON 개체 (예 : { "name": "value"})로 변경하거나 deserialized 형식을 배열 또는 List와 같은 컬렉션 인터페이스 (예 : 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;
}
낮은 기술 접근
용법
var input = "[[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]]";
var results = JArray
.Parse(input)
.Select(x => new Candle(){
OpenTime = DateTimeOffset.FromUnixTimeMilliseconds(x[0].Value<long>()).DateTime,
Open = x[1].Value<decimal>(),
High = x[2].Value<decimal>(),
Low = x[3].Value<decimal>(),
Close = x[4].Value<decimal>(),
Volume = x[5].Value<decimal>()
}).ToList();
foreach(var item in results)
Console.WriteLine($"Open : {item.Open}, High : {item.High}, Low : {item.Low}, Close : {item.Close}, Volume : {item.Volume}");
산출
Open : 0.02585, High : 0.02585, Low : 0.02577, Close : 0.02577, Volume : 2346260.5
Open : 0.02577, High : 0.02577, Low : 0.02571, Close : 0.02572, Volume : 3853038.7
Open : 0.02572, High : 0.02573, Low : 0.02568, Close : 0.02573, Volume : 2525735.5
Open : 0.02573, High : 0.02578, Low : 0.02573, Close : 0.02574, Volume : 2519284.4
Open : 0.02575, High : 0.02582, Low : 0.02574, Close : 0.02578, Volume : 1463562.6
Open : 0.02578, High : 0.02587, Low : 0.02577, Close : 0.02585, Volume : 2074134.3
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다