c# - Deserializing JSON string to Object with json.net -
i'm building small application pulls statistics api have no control over. json string looks this:
{ "weapons": [ { "aek": { "name":"aek-971 vintovka", "kills":47, "shots_fired":5406, "shots_hit":858 }, "xm8": { "name":"xm8 prototype", "kills":133, "shots_fired":10170, "shots_hit":1790 }, } ] }
and objects set follows:
class weapscollection { public weaponlist[] weapons { get; set; } } class weaponlist { public weapondetails aek { get; set; } public weapondetails xm8 { get; set; } } class weapondetails { public string name { get; set; } public int kills { get; set; } public int shots_fired { get; set; } public int shots_hit { get; set; } }
i don't have problems deserializing string way set now, i.e. can do:
weapscollection weps = jsonconvert.deserializeobject<weapscollection>(json); console.writeline(weps.weapons.first().aek.name.tostring()); console.readline();
this outputs aek-971 vintovka
fine... don't want different weapons separate properties. want able enumerate , foreach on each weapon or this:
console.writeline(weapons.where(w => w.kills > 30).name.tostring());
any tips how achieve this?
the list of actual weapons 60+ i've been thinking doing string.replace ("[weaponname]", "weapon") on json data before deserializing it, cannot work either.
i'd appreciate tips.
what remove square brackets json, turns array dictionary. deserialise directly class:
class weaponlist { public dictionary<string, weapondetails> weapons { get; set; } }
you can whatever want that.
Comments
Post a Comment