I am trying to deserialize this string:
[[1627948800000,"33361.77000000","33881.50000000","32832.75000000","32953.11000000","513.87963000",1627963199999,"17079364.00614616",18285,"267.93047400","8902854.13595806","0"],[1627963200000,"32958.11000000","33082.10000000","32350.00000000","32887.55000000","473.31029500",1627977599999,"15473902.88026993",19157,"218.09445000","7129688.95708971","0"],[1627977600000,"32889.44000000","33045.20000000","32650.48000000","32745.58000000","297.93607700",1627991999999,"9785835.12349423",14516,"151.51781200","4976292.20423003","0"]]
Into this object:
[JsonArray]
public class CandlestickData
{
[JsonProperty(Order = 1)]
public long OpenTime { get; set; }
[JsonProperty(Order = 2)]
public decimal Open { get; set; }
[JsonProperty(Order = 3)]
public decimal High { get; set; }
[JsonProperty(Order = 4)]
public decimal Low { get; set; }
[JsonProperty(Order = 5)]
public decimal Close { get; set; }
[JsonProperty(Order = 6)]
public decimal Volume { get; set; }
[JsonProperty(Order = 7)]
public long CloseTime { get; set; }
[JsonProperty(Order = 8)]
public decimal QuoteAssetVolume { get; set; }
[JsonProperty(Order = 9)]
public long NumberOfTrades { get; set; }
[JsonProperty(Order = 10)]
public decimal TakerBuyBaseAssetVolume { get; set; }
[JsonProperty(Order = 11)]
public decimal TakerBuyQuoteAssetVolume { get; set; }
[JsonProperty(Order = 12)]
public decimal Ignore { get; set; }
}
I've tried using JsonConvert.DeserializeObject<CandlestickData[]>(content); where content is the string mentioned above.
However, I get the following error:
Cannot create and populate list type CandlestickData.
I tried to changing the decimals to strings, but this gives me the same error. What's the right way to deserialize this string? I don't have control over the json that is created.
Order=on a JsonProperty does what you think it does. It defines the order in which props will be written during serializing; it's not a device for unpacking an array into different propspublic class RootBase { public List<List<object>> MyArray { get; set; } }=>RootBase myDeserializedClass = JsonConvert.DeserializeObject<RootBase>(content);otherwise follow what @CaiusJard mentions. Also worth mentioning, do you have control over creating that json, if so, change it so it'skey:valueso you actually have property names along with their values; deserializing and getting what you need would be a breeze.ObjectToArrayConverter<CandlestickData>whereObjectToArrayConverter<T>is shown in this answer to C#: Parsing a non-JSON array-only api response to a class object with x properties (which is a duplicate) and originally came from How to deserialize an array of values with a fixed schema to a strongly typed data class?.