[英]C# JavascriptSerializer inheritence
Using JavascriptSerializer, is there any way to deserialize json like : 使用JavascriptSerializer,有什么方法可以反序列化json:
{
items:[
{name:"item1", prop1:true, prop2:"prop"},
{name:"item2", prop1:true, prop3:"prop", prop4:"prop"}
]
}
Is it possible to deserialize using these classes : 是否可以使用这些类反序列化:
public abstract class Item
{
public String name {get; set;}
public bool prop1 {get; set;}
}
public class ItemA : Item
{
public String prop2 {get; set;}
}
public class ItemB : Item
{
public String prop3 {get; set;}
public String prop4 {get; set;}
}
Thanks for help ! 感谢帮助 !
No, you can't do what you want unless you went for a non-type safe approach like using dynamic
eg 不,除非您采用了非dynamic
类型的安全方法(例如,使用dynamic
方法)
public class Container
{
public List<dynamic> items { get; set; }
}
The problem is you have a list of mixed types which the JavascriptSerializer
doesn't support. 问题是您有JavascriptSerializer
不支持的混合类型的列表。
try this paste your json string here http://json2csharp.com/ and generate the class and do the code 尝试此操作将您的json字符串粘贴到http://json2csharp.com/并生成类并执行代码
public class Item
{
public string name { get; set; }
public bool prop1 { get; set; }
public string prop2 { get; set; }
public string prop3 { get; set; }
public string prop4 { get; set; }
}
public class RootObject
{
public List<Item> items { get; set; }
}
string sValue ="{items:[{name:"item1", prop1:true, prop2:"prop"},{name:"item2", prop1:true,
prop3:"prop", prop4:"prop"}]}
System.Web.Script.Serialization.JavaScriptSerializer ObjJSerializer = new
System.Web.Script.Serialization.JavaScriptSerializer();
var Data = ObjJSerializer.Deserialize<RootObject>(sValue);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.