Thursday, 15 January 2015

c# - deserializing an object with a generic list -



c# - deserializing an object with a generic list -

i trying deserialize object, type populated getting null list<sport>. ideas?

my classes:

class sports { public msgtypes type { get; set; } public list<sport> sport { get; set; } } class sport { public int id { get; set; } public int import_id { get; set; } public string name { get; set; } public int active { get; set; } public int order { get; set; } public int min_bet { get; set; } public int max_bet { get; set; } public int updated { get; set; } public string feed_type { get; set; } public string locale { get; set; } }

the command deserialization:

sports _sports = (sports) jsonconvert.deserializeobject<sports>(jsonobj);

this json object:

"{\"code\":0,\"type\":4,\"sports\":[{\"sport\":{\"id\":\"1\",\"import_id\":\"1\",\"name\":\"soccer\",\"active\":true,\"order\":\"1\",\"min_bet\":\"0\",\"max_bet\":\"0\",\"updated\":\"1403194889\",\"feed_type\":\"betradar\",\"locale\":\"en_us\"}},{\"sport\":{\"id\":\"2\",\"import_id\":\"5\",\"name\":\"tennis\",\"active\":true,\"order\":\"3\",\"min_bet\":\"0\",\"max_bet\":\"0\",\"updated\":\"1403194771\",\"feed_type\":\"betradar\",\"locale\":\"en_us\"}},{\"sport\":{\"id\":\"3\",\"import_id\":\"6\",\"name\":\"handball\",\"active\":true,\"order\":\"6\",\"min_bet\":\"0\",\"max_bet\":\"0\",\"updated\":\"1403152901\",\"feed_type\":\"betradar\",\"locale\":\"en_us\"}},{\"sport\":{\"id\":\"4\",\"import_id\":\"4\",\"name\":\"ice hockey\",\"active\":true,\"order\":\"4\",\"min_bet\":\"0\",\"max_bet\":\"0\",\"updated\":\"1403080245\",\"feed_type\":\"betradar\",\"locale\":\"en_us\"}},{\"sport\":{\"id\":\"7\",\"import_id\":\"2\",\"name\":\"basketball\",\"active\":true,\"order\":\"2\",\"min_bet\":\"0\",\"max_bet\":\"0\",\"updated\":\"1403194830\",\"feed_type\":\"betradar\",\"locale\":\"en_us\"}},{\"sport\":{\"id\":\"8\",\"import_id\":\"23\",\"name\":\"volleyball\",\"active\":true,\"order\":\"5\",\"min_bet\":\"0\",\"max_bet\":\"0\",\"updated\":\"1403194591\",\"feed_type\":\"betradar\",\"locale\":\"en_us\"}},{\"sport\":{\"id\":\"9\",\"import_id\":\"12\",\"name\":\"rugby\",\"active\":true,\"order\":\"7\",\"min_bet\":\"0\",\"max_bet\":\"0\",\"updated\":\"1403194710\",\"feed_type\":\"betradar\",\"locale\":\"en_us\"}},{\"sport\":{\"id\":\"12\",\"import_id\":\"11\",\"name\":\"motorsport\",\"active\":true,\"order\":\"12\",\"min_bet\":\"0\",\"max_bet\":\"0\",\"updated\":\"1403065699\",\"feed_type\":\"betradar\",\"locale\":\"en_us\"}},{\"sport\":{\"id\":\"13\",\"import_id\":\"3\",\"name\":\"baseball\",\"active\":true,\"order\":\"13\",\"min_bet\":\"0\",\"max_bet\":\"0\",\"updated\":\"1403194834\",\"feed_type\":\"betradar\",\"locale\":\"en_us\"}},{\"sport\":{\"id\":\"14\",\"import_id\":\"16\",\"name\":\"american football\",\"active\":true,\"order\":\"14\",\"min_bet\":\"0\",\"max_bet\":\"0\",\"updated\":\"1403143326\",\"feed_type\":\"betradar\",\"locale\":\"en_us\"}},{\"sport\":{\"id\":\"16\",\"import_id\":\"34\",\"name\":\"beach volley\",\"active\":true,\"order\":\"16\",\"min_bet\":\"0\",\"max_bet\":\"0\",\"updated\":\"1403194417\",\"feed_type\":\"betradar\",\"locale\":\"en_us\"}}]}"

you need 1 more level of nesting , different class names. should able deserialize such structure:

class sportsparent { //code msgtypes not provided commented out public list<sportgroup> sports { get; set; } } class sportgroup { public sportitem sport { get; set; } } class sportitem { public int id { get; set; } public int import_id { get; set; } public string name { get; set; } public bool active { get; set; } //need converted bool instead of int public int order { get; set; } public int min_bet { get; set; } public int max_bet { get; set; } public int updated { get; set; } public string feed_type { get; set; } public string locale { get; set; } }

you can deserialize using such code:

sportsparent _sports = jsonconvert.deserializeobject<sportsparent>(jsonobj);

c# json.net

No comments:

Post a Comment