Click here to Skip to main content
15,879,095 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I try to parse this below json string in class object

The Json String is:

{"formula":"MaxDate","parameters":[{"Value":"03/25/2014","Type":"Date"},{"Value":"22","Type":"Unit"}]}

And the class i create is:

C#
class CustomFormula
{
    public string formula { get; set; }
    public List<Parameters> parameters { get; set; }
}
public class Parameters
{
    public string Value { get; set; }
    public string Type { get; set; }
}


To parse the code is:

C#
JavaScriptSerializer JsonSerializer = new JavaScriptSerializer();
CustomFormula OCustomFormula =(CustomFormula)JsonSerializer.DeserializeObject(formulaValue.ToString());


But I am Getting the exception :

"Unable to cast object of type 'System.Collections.Generic.Dictionary`2[System.String,System.Object]' to type 'WDCS.MODELS.CustomFormula'."

Please help ?
Posted

1 solution

I use this code

C#
CustomFormula OCustomFormula = JsonSerializer.Deserialize<customformula>(formulaValue.ToString());


instead of

C#
CustomFormula OCustomFormula =(CustomFormula)JsonSerializer.DeserializeObject(formulaValue.ToString());
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900