0

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.

6
  • Not sure that 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 props Commented Aug 19, 2021 at 14:01
  • 1
    What's the right way to deserialize this string? - there are probably many, but making all your props JsonIgnore, deser'ing to a single object[] prop (not that props should return arrays but..) and having each prop take a different array index might work.. Other than that I think you're probably looking at a custom deser routine Commented Aug 19, 2021 at 14:04
  • public 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's key:value so you actually have property names along with their values; deserializing and getting what you need would be a breeze. Commented Aug 19, 2021 at 14:08
  • I don't have control over the json that is created. That is the issue. When I try to create a class Rootbase with a list of CandlestickData object, I get the error "Cannot create and populate list type RootBase. ". Commented Aug 19, 2021 at 14:15
  • 1
    Use ObjectToArrayConverter<CandlestickData> where ObjectToArrayConverter<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?. Commented Aug 19, 2021 at 14:15

1 Answer 1

-1

Your json text and your code is mismatched. You have a list of items in that json text and you have a single class in your code. I recommend you make a list of CandleStickData (like 3 of them) and then serialize it and examine the json text output. You will then need to alter your json text to match that format. Also get rid of those json attributes that you are using when you do this.

Sign up to request clarification or add additional context in comments.

1 Comment

In comments the querent states I don't have control over the json that is created. so they cannot alter [the] json text to match that format.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.