繁体   English   中英

使用JSON.NET反序列化具有奇数结构的JSON

[英]Deserializing JSON with odd structure with JSON.NET

我正在尝试使用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());
}

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

你不能做那样的事吗?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM