.net - C# parse JSON group by type -


this json

[     {         "name": "teama",         "zone": "north",         "id": 12,         "players": [             {                 "name": "taplayer1",                 "type": "keeper"             },             {                 "name": "taplayer2",                 "type": "bowler"             }         ]     },     {         "name": "teamb",         "zone": "south",         "id": 20,         "players": [             {                 "name": "tbplayer1",                 "type": "keeper"             }         ]     },     {         "name": "teamc",         "zone": "south",         "id": 45,         "players": null     },     {         "name": "teamd",         "zone": "north",         "id": 40,         "players": [             {                 "name": "tom",                 "type": "keeper"             },             {                 "name": "max",                 "type": "keeper"             },             {                 "name": "sam",                 "type": "bowler"             },             {                 "name": "jim",                 "type": "keeper"             }         ]     } ] 

i want result (group zone player type keeper)

north taplayer1 tom max  south tbplayer1 

i tried parse that

public class player     {         public string name { get; set; }         public string type { get; set; }     }      public class teams     {         public string name { get; set; }         public string zone { get; set; }         public int id { get; set; }         public list<player> players { get; set; }     }         teams[] ownersarray = jsonconvert.deserializeobject<teams[]>(json); 

i want display results group zone , type should keeper. suggestions appreciated.

thanks in advance.

after deserializing json

teams[] teams = jsonconvert.deserializeobject<teams[]>(json); 

you can use linq group zone

var groupedbyzone = teams.where(t => t.players != null).groupby(t => t.zone); 

from each group want players

var players = groupedbyzone.select(g => new {      name = g.key,      players = g.selectmany(t => t.players).where(p => p.type == "keeper").toarray()  }); 

this done in 1 statement

var keepersbyzone = teams.where(t => t.players != null)                          .groupby(team => team.zone)                          .select(g => new {                               name = g.key,                               players = g.selectmany(team => team.players)                                         .where(player => plapyer.type == "keeper")                                         .orderby(player => player.name)                                         .toarray()                           }); 

show output

foreach(var zone in keepersbyzone) {     console.writeline(zone.name);     foreach(var player in zone.players) {         console.writeline(player.name);     }     console.writeline(); } 

Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -