c# - deserialize json into .net object using json.net -
i having problem deserializing json string .net objects. have container class contains information external , there field call classtype
defined type of information , actual content in property, can anything, define object type.
following .net class definition helps understand issue.
class classone { public string name { get; set; } public int age { get; set; } } class classtwo { public string addressline { get; set; } public string addressline2 { get; set; } } class classthree { public string country { get; set; } public string passport { get; set; } } class containerclass { public string classtype { get; set; } public object classcontent { get; set; } }
when getting information external in json format like:
{"classtype":"class1","classcontent":{"name":"james","age":2}}
i using newtonsoft json.net library deserialize json string. seems default deserialize function deserialize newtonsoft.json.linq.jcontainer
. wondering how can write converter deserialize classcontent
based on classtype
definition. code sample highly appreciated.
i go dynamic
way, like:
string json = @"{""classtype"":""class1"",""classcontent"":{""name"":""james"",""age"":2}}"; dynamic jobj = jobject.parse(json); if (jobj.classtype == "class1") { console.writeline("{0} {1}", jobj.classcontent.name, jobj.classcontent.age); }
since returning object (classcontent) doesn't mean much, , have cast concrete class somehow (using if's or switch).
Comments
Post a Comment