Deserializing JSON data to C# using JSON.NET -
i'm relatively new working c# , json data , seeking guidance. i'm using c# 3.0, .net3.5sp1, , json.net 3.5r6.
i have defined c# class need populate json structure. however, not every json structure entry retrieved web service contains possible attributes defined within c# class.
i've been being doing seems wrong, hard way , picking out each value 1 one jobject , transforming string desired class property.
jsonserializer serializer = new jsonserializer(); var o = (jobject)serializer.deserialize(myjsondata); myaccount.employeeid = (string)o["employeeid"][0];
what best way deserialize json structure c# class , handling possible missing data json source?
my class defined as:
public class myaccount { [jsonproperty(propertyname = "username")] public string userid { get; set; } [jsonproperty(propertyname = "givenname")] public string givenname { get; set; } [jsonproperty(propertyname = "sn")] public string surname { get; set; } [jsonproperty(propertyname = "passwordexpired")] public datetime passwordexpire { get; set; } [jsonproperty(propertyname = "primaryaffiliation")] public string primaryaffiliation { get; set; } [jsonproperty(propertyname = "affiliation")] public string[] affiliation { get; set; } [jsonproperty(propertyname = "affiliationstatus")] public string affiliationstatus { get; set; } [jsonproperty(propertyname = "affiliationmodifytimestamp")] public datetime affiliationlastmodified { get; set; } [jsonproperty(propertyname = "employeeid")] public string employeeid { get; set; } [jsonproperty(propertyname = "accountstatus")] public string accountstatus { get; set; } [jsonproperty(propertyname = "accountstatusexpiration")] public datetime accountstatusexpiration { get; set; } [jsonproperty(propertyname = "accountstatusexpmaxdate")] public datetime accountstatusexpirationmaxdate { get; set; } [jsonproperty(propertyname = "accountstatusmodifytimestamp")] public datetime accountstatusmodified { get; set; } [jsonproperty(propertyname = "accountstatusexpnotice")] public string accountstatusexpnotice { get; set; } [jsonproperty(propertyname = "accountstatusmodifiedby")] public dictionary<datetime, string> accountstatusmodifiedby { get; set; } [jsonproperty(propertyname = "entrycreatedate")] public datetime entrycreatedate { get; set; } [jsonproperty(propertyname = "entrydeactivationdate")] public datetime entrydeactivationdate { get; set; } }
and sample of json parse is:
{ "givenname": [ "robert" ], "passwordexpired": "20091031041550z", "accountstatus": [ "active" ], "accountstatusexpiration": [ "20100612000000z" ], "accountstatusexpmaxdate": [ "20110410000000z" ], "accountstatusmodifiedby": { "20100214173242z": "tdecker", "20100304003242z": "jsmith", "20100324103242z": "jsmith", "20100325000005z": "rjones", "20100326210634z": "jsmith", "20100326211130z": "jsmith" }, "accountstatusmodifytimestamp": [ "20100312001213z" ], "affiliation": [ "employee", "contractor", "staff" ], "affiliationmodifytimestamp": [ "20100312001213z" ], "affiliationstatus": [ "detached" ], "entrycreatedate": [ "20000922072747z" ], "username": [ "rjohnson" ], "primaryaffiliation": [ "staff" ], "employeeid": [ "999777666" ], "sn": [ "johnson" ] }
use jsonconvert.deserializeobject<rootobject>(string json);
create classes on json 2 c#
json.net documentation: serializing , deserializing json json.net
Comments
Post a Comment