0

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?

1
  • My current work-around is to modify the original JSON as follows: result = result.Replace("return\":{\"", ""); result = result.Substring(0, result.Length - 1); ...this works, but it's definitely a hack. I'd prefer a cleaner solution if there is one! Commented Aug 14, 2013 at 14:07

1 Answer 1

1

Can't you do something like that?

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

and then access you data like that:

foreach(SellOrder sellorder in desObj.Container.SellOrders)
{
    Console.WriteLine(sellorder.total.ToString());
}
Sign up to request clarification or add additional context in comments.

2 Comments

This worked - thanks. Would be nice if there was a clever solution using the JsonProperty Annotation, since the entire API I'm working with does this on every request. Thanks again.
I don't think there is any way to skip the return property in Json using attributes :-(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.