1

trying to convert below JSON into a c# object with newtonsoft json converter. The problem comes when trying to resolve attribute "b"/"a" into a class attribute. It's an array of array's that contains 2 strings and an empty array. Empty array will be ignored.

Json:

{
        "e": "depthUpdate",                     // event type
        "E": 1499404630606,                     // event time
        "s": "ETHBTC",                          // symbol
        "u": 7913455,                           // updateId 
        "b": [                                  // bid depth delta
            [
                "0.10376590",                   // price 
                "59.15767010",                  // quantity
                []                              // can be ignored
            ],
        ],
        "a": [                                  // ask depth delta
            [
                "0.10376586",                   // price
                "159.15767010",                 
                []                              
            ],
            [
                "0.10383109",
                "345.86845230",
                []
            ],
            [
                "0.10490700",
                "0.00000000",                   //quantitiy
                []
            ]
        ]
    }

Current C# class to convert it to:

class DepthInformation
{
    [JsonProperty("e")]
    public string eventType { get; set; }
    [JsonProperty("E")]
    public string eventTime { get; set; }
    [JsonProperty("s")]
    public string symbol { get; set; }
    [JsonProperty("u")]
    public int updateId { get; set; }
    //[JsonProperty("b")]
    //public IList<string> bids { get; set; }

Would love for the "a"/"b" attributes to be an array of type Bids class within the DepthInformation class something like:

class DepthInformation
{
    [JsonProperty("e")]
    public string eventType { get; set; }
    [JsonProperty("E")]
    public string eventTime { get; set; }
    [JsonProperty("s")]
    public string symbol { get; set; }
    [JsonProperty("u")]
    public int updateId { get; set; }
    [JsonProperty("b")]
    public IList<Bid> bids { get; set; }
}

[JsonArray("b")]
public class Bid
{
    // No identifiers here so not sure how to map json to these
    decimal price { get; set; }
    decimal quantity { get; set; }
}

I'm lost, appreciate any help!

CuriousGeorge
  • 519
  • 3
  • 12
  • For `Bid` you should be able to use `ObjectToArrayConverter` from [C# JSON.NET - Deserialize response that uses an unusual data structure](https://stackoverflow.com/q/39461518/3744182). Then you could make `bids` be an `IList`. – dbc Dec 19 '17 at 04:12
  • 1
    Thanks exactly what I needed. This is duplicated, will remove – CuriousGeorge Dec 19 '17 at 04:38
  • 1
    You don't need to remove it if you don't want to -- it's a perfectly good question that happened to be answered by some other question's answer. – dbc Dec 19 '17 at 04:40

0 Answers0