JSON.NET转换具有不同类型的数组数组

时间:2017-12-19 04:07:52

标签: c# json json.net

尝试使用newtonsoft json转换器将以下JSON转换为c#对象。尝试将属性“b”/“a”解析为类属性时出现问题。它是一个包含2个字符串和一个空数组的数组数组。空数组将被忽略。

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
                []
            ]
        ]
    }

当前的C#类将其转换为:

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; }

希望“a”/“b”属性成为DepthInformation类中类型Bids类的数组,如:

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; }
}

我迷路了,感谢任何帮助!

0 个答案:

没有答案