Last active
August 29, 2015 14:00
-
-
Save timheuer/2309e689a40a20f3e8c9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
JSON: | |
[ | |
{ | |
team: { | |
international_date: false, | |
is_coed: false, | |
team_name: "foo bar" | |
} | |
}, | |
{ | |
team: { | |
international_date: false, | |
is_coed: false, | |
team_name: "foo bar" | |
} | |
} | |
] | |
C# Class: | |
[JsonObject("team")] | |
public class Team | |
{ | |
[JsonProperty("international_date")] | |
public bool InternationalDate { get; set; } | |
[JsonProperty("is_coed")] | |
public bool IsCoed { get; set; } | |
[JsonProperty("team_name")] | |
public string TeamName { get; set; } | |
} | |
Code: | |
List<Team> teams = (List<Team>) Newtonsoft.Json.JsonConvert.DeserializeObject<List<Team>>(data); |
I bet Aaron's suggestion would work... an alternative if you can change the JSON would be what I've pasted below:
[
{
international_date: false,
is_coed: false,
team_name: "foo bar"
},
{
international_date: false,
is_coed: false,
team_name: "foo bar"
}
]
Remove lines 4,8,11,15 ... no need to explicitly define each team record as your passing an array in for deserialization anyway, each array element is a "team" ..
The JSON array is of anonymous objects each with a team object as a property. The list class needs to match, something like:
[JsonObject("team")]
public class Team
{
[JsonProperty("international_date")]
public bool InternationalDate { get; set; }
[JsonProperty("is_coed")]
public bool IsCoed { get; set; }
[JsonProperty("team_name")]
public string TeamName { get; set; }
}
public class TeamWrapper
{
[JsonProperty("team")]
public Team Team { get; set; }
}
...
List<TeamWrapper> teams = (List<TeamWrapper>)Newtonsoft.Json.JsonConvert.DeserializeObject<List<TeamWrapper>>(data);
Oops I see @aaronlerch had it already, sorry!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So you don't control the JSON format? Looks kind of like an array of objects, each with a team property set to an object with international_date, is_coed, etc.