无法将当前JSON数组反序列化为类型

som*_*ley -1 c# json json.net deserialization

我正在尝试反序列化以下JSON数据:

{
  "bids": [
    [
      "392031.00000000",
      "0.00254444"
    ],
    [
      "390000.00000000",
      "0.52917503"
    ],
     ......
    ],

   "asks": [
    [
      "392999.00000000",
      "1.00000000"
    ],
    [
      "393000.00000000",
      "0.31572236"
    ],
    .....
    ]
}
Run Code Online (Sandbox Code Playgroud)

我已经研究过类似的问题,比如这一个,但我没有得到接近的结果,也注意到,在这个问题JSON的结构并不类似.

我的反序列化代码如下

public class OrderBookElement
{
    // I've also tried below commented code
    //[JsonProperty(PropertyName = "0")]
    //public double price { get; set; }

    //[JsonProperty(PropertyName = "1")]
    //public double volume { get; set; }

    List<double> values;
}
public class OrderBookResponse
{
    [JsonProperty(PropertyName = "bids")]
    List<OrderBookElement> bids { get; set; }
    [JsonProperty(PropertyName = "asks")]
    List<OrderBookElement> asks { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

下面是我用于反序列化的代码

var ordBook = JsonConvert.DeserializeObject<OrderBookResponse>(jsonResponse);
Run Code Online (Sandbox Code Playgroud)

这给了我错误:

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'RestAPI.OrderBookElement' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. 
Run Code Online (Sandbox Code Playgroud)

Jon*_*eet 6

您展示的JSON将代表:

public class OrderBookResponse
{
    [JsonProperty("bids")]
    public List<List<string>> Bids { get; set; }

    [JsonProperty("asks")]
    public List<List<string>> Asks { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

JSON中的bidsasks属性都只是一个字符串数组的数组.

我建议反序列化到与JSON匹配的模型,然后将其转换为更有用的模型.这可能是可能的属性应用到你的类劝说Json.NET做你想要什么,但我倾向于认为,到时候有一个显著差异(不仅仅是属性名称),这是值得的两种模式分离出来,一个用于序列化,另一个用于代码的其余部分.