c# - Deserializing Json into different object types with Json.net -
this question has answer here:
the problem have different types of objects (it know how handle 1 type): this:
{"myobjects": [ { "type":"sampletype1", "name":"samplename1", "size":"samplesize1" }, { "type":"sampletype2", "name":"samplename2", "size":"samplesize2" } ] }
i handle just:
jsonconvert.deserializeobject<myobjectcontainer>(json);
where myobjectcontainer has property:
public list<myobjects> myobjects { get; set; }
the problem comes when object optional parameters:
{"myobjects": [ { "type":"sampletype1", "name":"samplename1", "size":"samplesize1" }, { "type":"sampletype2", "name":"samplename2", "size":"samplesize2", "additionalinfo":"additionalinfo" } ] }
to serialize easy think. i'll create class additionalinfoobject : myobject property string additionalinfo.
but how deserialize such json list?
you can tell json serializer ignore property, if null. mind ignore every property that's null. if initialized default value other that, won't work.
here's exmaple: how ignore property in class if null, using json.net
Comments
Post a Comment