如何反序列化json以便将某些json属性分配给数组或List?

时间:2015-08-25 06:17:55

标签: c# json.net

使用Json.Net,如何将以下json反序列化为下面提供的OrderDepthFeed类?我希望bid1进入Bid [0],bid2进入Bid [1],依此类推。

{
    "i": "101",
    "m": 11,
    "tick_timestamp": 1440479701986,
    "bid1": 78.00,
    "bid_volume1": 60,
    "bid_orders1": 1,
    "ask1": 80.50,
    "ask_volume1": 500,
    "ask_orders1": 1,
    "bid2": 77.50,
    "bid_volume2": 500,
    "bid_orders2": 1,
    "ask2": 82.00,
    "ask_volume2": 1560,
    "ask_orders2": 2,
    "bid3": 77.00,
    "bid_volume3": 107,
    "bid_orders3": 2,
    "ask3": 82.95,
    "ask_volume3": 75,
    "ask_orders3": 1,
    "bid4": 76.30,
    "bid_volume4": 200,
    "bid_orders4": 1,
    "ask4": 83.40,
    "ask_volume4": 49,
    "ask_orders4": 1,
    "bid5": 76.00,
    "bid_volume5": 506,
    "bid_orders5": 2,
    "ask5": 83.50,
    "ask_volume5": 1500,
    "ask_orders5": 1
}

-

public class OrderDepthFeed {
    public int I { get; set; }
    public int M { get; set; }
    public int TickTimestamp { get; set; }

    public double?[] Ask { get; set; }
    public int?[] AskVolume { get; set; }
    public double?[] Bid { get; set; }
    public int?[] BidVolume { get; set; }
}

这里是我使用的代码,PascalCaseContractResolver只更改大小写,并通过重写ResolvePropertyName();删除属性名称中的下划线;

var serializerSettings = new JsonSerializerSettings();
serializerSettings.ContractResolver = new PascalCaseContractResolver();
var orders = JsonConvert.DeserializeObject<OrderDepthFeed>(json, serializerSettings);

0 个答案:

没有答案