Using JavascriptSerializer, is there any way to deserialize json like :
{
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
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.
try this paste your json string here http://json2csharp.com/ and generate the class and do the code
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.