I'm trying to deserialize some JSON into an object using JSON.NET's JsonConvert class.
Code I'm using with a JSON structure sample:
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\"}]}}");
And my market class:
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; }
}
The thing that's causing me problems is the fact that the data is under the 'return' key. If I remove the return key, this works perfectly. How would I go about getting my market object to behave like this:
foreach(SellOrder sellorder in desObj.SellOrders)
{
Console.WriteLine(sellorder.total.ToString());
}
I've tried experimenting with making the return attribute a dynamic list, then retrieving the sell/buy-orders that way, but nothing seems to work. Any ideas?