一尘不染

如何使用JsonConvert反序列化此JSON?

json

我有此JSON,但无法弄清楚如何将其转换为C#中的对象列表。

这是JSON:

{
  "2": {
    "sell_average": 239,
    "buy_average": 238,
    "overall_average": 240,
    "id": 2
  },
  "6": {
    "sell_average": 184434,
    "buy_average": 182151,
    "overall_average": 189000,
    "id": 6
  },
  "8": {
    "sell_average": 11201,
    "buy_average": 1723,
    "overall_average": 180,
    "id": 8
  }
}

我尝试使用的代码:

public class ItemSummaryModel
{
    public string Id { get; set; }
    public ItemSummary ItemSummary { get; set; }
}

public class ItemSummary
{
    public int Sell_Average { get; set; }
    public int Buy_Average { get; set; }
    public int Overall_Average { get; set; }
    public int Id { get; set; }
}

List<ItemSummaryModel> models =
    JsonConvert.DeserializeObject<List<ItemSummaryModel>>(jsonSummary);

无济于事。如何使用Newtonsoft的JSON库(Json.Net)将JSON反序列化为这些对象的列表?


阅读 327

收藏
2020-07-27

共1个答案

一尘不染

您可以使用

var dict = JsonConvert.DeserializeObject<Dictionary<int, ItemSummary>>(json);
var items = dict.Values.ToList();  //if you want a List<ItemSummary>;
2020-07-27