Json Mapping and serializing in C# -
i'm trying map json string similar in c# object. purpose send cilent side (it's jquery application) wondering whether i'm doing right ?
{"tasks":[ { "id":-1, "name":"gantt editor", "code":"", "level":0, "status":"status_active", "start":1372608000000, "duration":21, "end":1375113599999, "startismilestone":true, "endismilestone":false, "collapsed":false, "assigs":[] }, { "id":"tmp_fk1372575559620", "name":"release", "code":"", "level":1, "status":"status_active", "start":1372608000000, "duration":1, "end":1372694399999, "startismilestone":false, "endismilestone":false, "collapsed":false, "assigs":[] } ], "selectedrow":8, "deletedtaskids":[], "resources": [ { "id":"tmp_1", "name":"resource 1" } ], "roles":[ { "id":"tmp_1", "name":"project manager" } ], "canwrite":true, "canwriteonparent":true } and how mapped
public class attributes { public list<task> _task { get; set; } public list<resource> _resource { get; set; } public list<role> _role { get; set; } public bool _canwrite { get; set; } //"canwrite":true, public bool _canwriteonparent { get; set; } //"canwriteonparent":true, public bool _selectedrow { get; set; } //"selectedrow":0, public string [] _deletedtaskids { get; set; } //"deletedtaskids":[], } public class task { private string _id { get; set; } private string _name { get; set; } private string _code { get; set; } private int _level { get; set; } private string _status { get; set; } private int _start { get; set; } //“start”:1348696800000, private int _duration { get; set; } //“duration”:10, private int _end { get; set; } //“end”:1349906399999, private bool _startismilestone { get; set; } //“startismilestone”:false, private bool _endismilestone { get; set; } //“endismilestone”:false, public list<assign> _assigns { get; set; } //“assigs”:[…], private string _depends { get; set; } //“depends”:”7:3,8″, private string _description { get; set; } //“description”:”approval of testing”, private int _progress { get; set; } //“progress”:20 } public class assign { private string _resourceid { get; set; } //“resourceid”:”tmp_1″, private string _id { get; set; } //“id”:”tmp_1345560373990″, private string _roleid { get; set; } //“roleid”:”tmp_1″, private string _effort { get; set; } //“effort”:36000000 } public class resource { private string _id { get; set; } //“id”:”tmp_1″, private string _name { get; set; } //“name”:”resource 1″ } public class role { private string _id { get; set; } //“id”:”tmp_1″, private string _name { get; set; } //“name”:”project manager” } if i'm doing right, how can serialize object , send in order send client side ?
here link fantastic tool maps json c#, here mapping should like:
public class task { public object id { get; set; } public string name { get; set; } public string code { get; set; } public int level { get; set; } public string status { get; set; } public object start { get; set; } public int duration { get; set; } public object end { get; set; } public bool startismilestone { get; set; } public bool endismilestone { get; set; } public bool collapsed { get; set; } public list<object> assigs { get; set; } } public class resource { public string id { get; set; } public string name { get; set; } } public class role { public string id { get; set; } public string name { get; set; } } public class rootobject { public list<task> tasks { get; set; } public int selectedrow { get; set; } public list<object> deletedtaskids { get; set; } public list<resource> resources { get; set; } public list<role> roles { get; set; } public bool canwrite { get; set; } public bool canwriteonparent { get; set; } } there slick way of mapping json c# (i believe comes visual studio 2012 -> web essentials), can go edit-> paste special-> paste json classes
edit
since you've asked using json.net (you can nuget) can deserialize json this:
rootobject deserialized = jsonconvert.deserializeobject<rootobject>(data);
Comments
Post a Comment