1

I am using Json.NET for converting a JSON string, and I am just pasting a small string here:

"incentive_type":{"cost":0,"id":4}

"incentive_type":{"cost":{"points":400,"denom":null,"acc":{"foil":{},"rates":{}}},"id":4}

I have declared incentive_type as:

public class incentive_type
{
    public cost cost { get; set; }
    public int id { get; set; }
}

public class cost
{
    public cost()
    {
    }
    public Dictionary<string,int> points { get; set; }
    public Dictionary<string,string> denom { get; set; }
    public acc acc { get; set; }
}

public class acc
{
    public Dictionary<string,int> foil{ get; set; }
    public Dictionary<string,int> rates { get; set; }
}

While deserializing I am getting

cannot convert 0 to type'cost'

The reason is the first JSON string which has 0 for class. How can I set my class to handle this value?

Syscall
  • 19,327
  • 10
  • 37
  • 52
sunilr
  • 19
  • 3
  • Do you have access to the source of the JSON? That seems to me not to be the most consistent behavior for an API--if you're generating it yourself, perhaps just omit the cost if you intend a null. – Marc Bollinger Jun 29 '10 at 18:12
  • unfortunately i am getting json from third party which i cannot alter – sunilr Jun 29 '10 at 18:16

0 Answers0