这个Json应该是什么类结构? [英] What should be the class structure for this Json?

查看:71
本文介绍了这个Json应该是什么类结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在类对象中解析下面的json字符串



Json字符串是:



{formula:MaxDate,parameters:[{Value:03/25/2014,Type:Date},{Value:22,Type: 单位}}}



我创建的课程是:



  CustomFormula 
{
public 字符串公式{获取; set ; }
public 列表<参数>参数{ get ; set ; }
}
public class 参数
{
public string 值{ get ; set ; }
public string 输入{ get ; set ; }
}





要解析代码是:



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





但是我得到了例外:



无法将类型为'System.Collections.Generic.Dictionary`2 [System.String,System.Object]'的对象强制转换为'WDCS.MODELS .CustomFormula'。



请帮忙?

解决方案

我用这段代码



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





而不是



 CustomFormula OCustomFormula =(CustomFormula)JsonSerializer.DeserializeObject(formulaValue.ToString() ); 


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:

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:

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 ?

解决方案

I use this code

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



instead of

CustomFormula OCustomFormula =(CustomFormula)JsonSerializer.DeserializeObject(formulaValue.ToString());


这篇关于这个Json应该是什么类结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆