Reputation: 31
I get Match data from a server with json format. in this json, There are two properties (key) whose names are variable and dynamic and depending on the fixture. for example, I get Match data for fixture_id 256
{
"api": {
"results": 1,
"fixtures": [
{
"fixture_id": 256,
"league_id": 21,
"homeTeam": {
"team_id": 12,
"team_name": "Tottenham"
},
"awayTeam": {
"team_id": 13,
"team_name": "Everton"
},
"lineups": {
"Tottenham": {
"coach": "qqqq",
"formation": "4-2-3-1"
},
"Everton": {
"coach": "rrrr",
"formation": "4-2-3-1"
}
}
}
]
}
}
the class for this json
public class rootMatch
{
[JsonProperty("api")]
public Api Api { get; set; }
}
public class Api
{
[JsonProperty("results")]
public long Results { get; set; }
[JsonProperty("fixtures")]
public List<Fixture> Fixtures { get; set; }
}
public partial class Fixture
{
[JsonProperty("fixture_id")]
public long FixtureId { get; set; }
[JsonProperty("league_id")]
public long LeagueId { get; set; }
[JsonProperty("homeTeam")]
public Team HomeTeam { get; set; }
[JsonProperty("awayTeam")]
public Team AwayTeam { get; set; }
[JsonProperty("lineups")]
public Lineups Lineups { get; set; }
}
public class Lineups
{
[JsonProperty("Tottenham")]
public Team Tottenham{ get; set; }
[JsonProperty("Everton")]
public Team Everton{ get; set; }
}
public class Team
{
[JsonProperty("coach")]
public string Coach { get; set; }
[JsonProperty("formation")]
public string Formation { get; set; }
}
But, the rootMatch class work only, for this json.
Is there a way to change name of Data property during deserialize, change it to static name?
First TeamName at lineups -> change to the HomeTeam
and
Second TeamName at lineups -> change to the AwayTeam
for example, in this json "lineups"
(Tottenham convert -> HomeTeam)
and
(Everton convert -> AwayTeam)
Upvotes: 1
Views: 2329
Reputation: 106826
The "problematic" JSON is the object with two properties where the property names are the team names. These are not known in advance. To handle this you can change the type to a dictionary with a string key.
To create meaningful names I adjusted your types a bit:
This is team:
public class Team
{
[JsonProperty("team_id")]
public int TeamId { get; set; }
[JsonProperty("team_name")]
public string TeamName { get; set; }
}
and this is a lineup:
public class Lineup
{
[JsonProperty("coach")]
public string Coach { get; set; }
[JsonProperty("formation")]
public string Formation { get; set; }
}
The fixture then becomes:
public partial class Fixture
{
[JsonProperty("fixture_id")]
public long FixtureId { get; set; }
[JsonProperty("league_id")]
public long LeagueId { get; set; }
[JsonProperty("homeTeam")]
public Team HomeTeam { get; set; }
[JsonProperty("awayTeam")]
public Team AwayTeam { get; set; }
[JsonProperty("lineups")]
public Dictionary<string, Lineup> Lineups { get; set; }
}
Notice how the lineups
JSON property is mapped to a Dictionary<string, Lineup>
in C#.
Here is how you can get the lineups for the home and away teams:
var root = JsonConvert.DeserializeObject<rootMatch>(json);
var fixture = root.Api.Fixtures.First();
var homeTeamLineup = fixture.Lineups[fixture.HomeTeam.TeamName];
var awayTeamLineup = fixture.Lineups[fixture.AwayTeam.TeamName];
The team names are used as keys in the dictionary.
I use First
to pick the first fixture. You would probably use LINQ or a loop to process all the fixtures.
Upvotes: 1
Reputation: 45101
You can add a property
[JsonExtensionData]
public Dictionary<string, object> AdditionalData { get; set; }
This will contain all properties that couldn't be match. Then you could iterate over the dictionary and process this data as you like.
Upvotes: 1