Skip to content

Instantly share code, notes, and snippets.

@timheuer
Last active August 29, 2015 14:00
Show Gist options
  • Save timheuer/2309e689a40a20f3e8c9 to your computer and use it in GitHub Desktop.
Save timheuer/2309e689a40a20f3e8c9 to your computer and use it in GitHub Desktop.
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);
@liquidboy
Copy link

that middle brace on line 9 is not supposed to be there?

@timheuer
Copy link
Author

This is the JSON that I'm getting

@aaronlerch
Copy link

Not able to test at the moment, but I wonder if you're using [JsonObject] correctly? The docs seem to indicate it doesn't do what you mean it to do here:
http://james.newtonking.com/json/help/index.html?topic=html/SerializationAttributes.htm

Because without that attribute working the way it's implied to work in your code, then your result would be a list of objects that have a "team" property that could be an object of type team.

So if you had:

public class TeamContainer // <-- horrible name, ignore
{
    public Team team { get; set; }
}

Then I think this should work:

List<TeamContainer> teams = (List<TeamContainer>) Newtonsoft.Json.JsonConvert.DeserializeObject<List<TeamContainer>>(data);

@pstephens
Copy link

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.

@DigitalJustin
Copy link

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"
}

]

@liquidboy
Copy link

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" ..

@msalsbery
Copy link

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);

@msalsbery
Copy link

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