我正在尝试使用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());
}
我尝试过将返回属性设为动态列表,然后以这种方式检索卖出/买入订单,但似乎没有任何效果。有什么想法吗?
答案 0 :(得分: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());
}