c# - Parse dynamic JSON -
i using c# , json.net 9.0.1
i have following json
{ "lineups": [ { "55": { "id": "55", "game_id": "1", "player_id": "55", "jersey_number": "78" }, "56": { "id": "56", "game_id": "1", "player_id": "56", "jersey_number": "77" }, "57": { "id": "57", "game_id": "1", "player_id": "57", "jersey_number": "76" } } ] }
all of array items of type player. how can parse json each item "55", "56", "57" stored in list of players, list?
the source json can't modified coming 3rd party.
update modified json valid,
if created 2 classes this:
public class lineup { public list<dictionary<string,player>> lineups; } public class player { public string id {get; set; } public string game_id { get; set;} public string player_id {get;set;} public string jersey_number {get;set;} }
then should (after you've fixed invalid json), able deserialize this:
var l = newtonsoft.json.jsonconvert.deserializeobject<lineup>(source);
example: https://dotnetfiddle.net/e7euuz
you can use various attributes in json.net customize deserialization. example, might want use jsonpropertyattribute
map names in json source c# property names match standard.
Comments
Post a Comment