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.

dbc
  • 104,963
  • 20
  • 228
  • 340
Ron Splinter
  • 151
  • 3
  • 14
  • 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 – Caius Jard Aug 19 '21 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 – Caius Jard Aug 19 '21 at 14:04
  • `public class RootBase { public List> MyArray { get; set; } }` => `RootBase myDeserializedClass = JsonConvert.DeserializeObject(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. – Trevor Aug 19 '21 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. ". – Ron Splinter Aug 19 '21 at 14:15
  • 1
    Use `ObjectToArrayConverter` where `ObjectToArrayConverter` is shown in [this answer](https://stackoverflow.com/a/48431099/3744182) to [C#: Parsing a non-JSON array-only api response to a class object with x properties](https://stackoverflow.com/q/48429702/3744182) (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?](https://stackoverflow.com/a/39462464/3744182). – dbc Aug 19 '21 at 14:15
  • *I don't have control over the json that is created* - no reason you can't carry out some basic string replacements to make it easier to parse, by the way, but I really do think i'd just deser this to a 2D array and then LINQ Select to get candlestickdatas – Caius Jard Aug 21 '21 at 07:28

1 Answers1

-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.

ashlar64
  • 1,054
  • 1
  • 9
  • 24
  • 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.* – dbc Aug 19 '21 at 14:20