0

我正在尝试使用 JSON.NET 的 JsonConvert 类将一些 JSON 反序列化为对象。

我与 JSON 结构示例一起使用的代码:

var desObj = JsonConvert.DeserializeObject<Market>("{\"success\":\"1\",\"return\":
{\"sellorders\":
[{\"sellprice\":\"0.00001099\",\"quantity\":\"60.00000000\",\"total\":\"0.00065940\"},
{\"sellprice\":\"0.00001100\",\"quantity\":\"1000.00000000\",\"total\":\"0.01100000\"},
{\"sellprice\":\"0.00001105\",\"quantity\":\"60.00000000\",\"total\":\"0.01200\"}]}}");

我的市场类别:

class Market
    {
        [JsonProperty("success")]
        public int Success { get; set; }

        [JsonProperty("sellorders")]
        public List<SellOrder> SellOrders {get; set;}

        [JsonProperty("buyorders")]
        public List<BuyOrder> BuyOrders {get; set;}
    }

    public class SellOrder
    {
        [JsonProperty("sellprice")]
        public decimal SellPrice { get; set; }

        [JsonProperty("quantity")]
        public decimal Quantity { get; set; }

        [JsonProperty("total")]
        public decimal Total { get; set; }
    }

    public class BuyOrder
    {
        [JsonProperty("buyprice")]
        public decimal BuyPrice { get; set; }

        [JsonProperty("quantity")]
        public decimal Quantity { get; set; }

        [JsonProperty("total")]
        public decimal Total { get; set; }
    }

导致我出现问题的原因是数据位于“返回”键下。如果我删除返回键,这将完美运行。我将如何让我的市场对象表现得像这样:

foreach(SellOrder sellorder in desObj.SellOrders)
{
    Console.WriteLine(sellorder.total.ToString());
}

我尝试过将返回属性设为动态列表,然后以这种方式检索卖出/买入订单,但似乎没有任何效果。有任何想法吗?

4

1 回答 1

1

你不能做这样的事吗?

class Market
    {
      [JsonProperty("success")]
      public int Success { get; set; }
      [JsonProperty("return")]
      public Container Container { get; set; }

    }
    class Container
    {
      [JsonProperty("sellorders")]
      public List<SellOrder> SellOrders { get; set; }

      [JsonProperty("buyorders")]
      public List<BuyOrder> BuyOrders { get; set; }
    }

    public class SellOrder
    {
      [JsonProperty("sellprice")]
      public decimal SellPrice { get; set; }

      [JsonProperty("quantity")]
      public decimal Quantity { get; set; }

      [JsonProperty("total")]
      public decimal Total { get; set; }
    }

    public class BuyOrder
    {
      [JsonProperty("buyprice")]
      public decimal BuyPrice { get; set; }

      [JsonProperty("quantity")]
      public decimal Quantity { get; set; }

      [JsonProperty("total")]
      public decimal Total { get; set; }
}

然后像这样访问您的数据:

foreach(SellOrder sellorder in desObj.Container.SellOrders)
{
    Console.WriteLine(sellorder.total.ToString());
}
于 2013-08-14T14:08:45.577 回答