lichtbringer
lichtbringer

Reputation: 118

Deserialize JSON with dynamic properties with JSON.NET in C#

I've got a JSON that, for example, looks like

{"historymatch":
   {
    "id":[1581402],
    "mid":[166],
    "aid":[5621],
    "bid":[18548],
    "date":["24/05/16"],
    "liveA":[3],
    "liveB":[1],
    "redA":[0],
    "redB":[0],
    "bc":["3-0"],
    "ng":[0],
    "rq":["-1.5"],
    "rql":[0],
    "worl":[0],
    "oworl":[0]},
    "match":
    {
        "166":
        {
           "n":"INT CF",
           "c":"5691D8"
        }
    },
    "team":
    {
       "5621":"Melbourne Knights",
       "18548":"Dandenong City SC"
    },
    "note":{}
}

As you can see there are dynamic properties and field in match and in team. I tried to reflect this in my c# classes

class History<T> where T : new()
{
    public HistoryMatch<T> HistoryMatch { get; set; }
}
class HistoryMatch<T> where T: new()
{
    [JsonProperty("id")]
    public IList<long> MatchId { get; set; }
    [JsonProperty("mid")]
    public IList<long> LeagueId { get; set; }
    [JsonProperty("aid")]
    public IList<long> TeamAId { get; set; }


 [JsonProperty("bid")]
    public IList<long> TeamBId { get; set; }
    [JsonProperty("date")]
    public IList<string> MatchDate { get; set; }
    [JsonProperty("liveA")]
    public IList<long> TeamAScore { get; set; }
    [JsonProperty("liveB")]
    public IList<long> TeamBScore { get; set; }
    [JsonProperty("redA")]
    public IList<long> TeamARedCard { get; set; }
    [JsonProperty("redB")]
    public IList<long> TeamBRedCard { get; set; }
    [JsonProperty("bc")]
    public IList<string> HalfTimeScore { get; set; }
    [JsonProperty("ng")]
    public IList<long> Ng { get; set; }
    [JsonProperty("rq")]
    public IList<string> Rq { get; set; }
    [JsonProperty("rql")]
    public IList<long> Rql { get; set; }
    [JsonProperty("worl")]
    public IList<long> Worl { get; set; }
    [JsonProperty("oworl")]
    public List<long> Oworl { get; set; }
    [JsonProperty("match")]
    public Dictionary<string,T> Match { get; set; }
    [JsonProperty("team")]
    public Team Team { get; set; }
    [JsonProperty("note")]
    public Note Note { get; set; }
}

class Match
{
    [JsonProperty("n")]
    public string Name { get; set; }
    [JsonProperty("c")]
    public string Color { get; set; }
}

class Team
{
    public Dictionary<string, string> Values { get; set; }
}

class Note
{

}

Then I deserialize it the following way

var obj = JsonConvert.DeserializeObject<History<Match>>(responseText);

It is deserialized without an error but the Attributes Match and Team of the class HistoryMatch are null afterwards. I've taken several attemps now but it seems that I'm missing something essential here. Could someone please give me an advise?

Upvotes: 1

Views: 718

Answers (1)

Maksim Simkin
Maksim Simkin

Reputation: 9679

In your Json Team, Match and Notes are not parts of the HistoryMatch, but parts of the History. Define your History class this way, i did it an i have deserialized your team and match values.

class History<T> where T : new()
{
    public HistoryMatch<T> HistoryMatch { get; set; }
    [JsonProperty("match")]
    public Dictionary<string, T> Match { get; set; }
    [JsonProperty("team")]
    public Dictionary<string, string> Team { get; set; }
    [JsonProperty("note")]
    public Note Note { get; set; }
}

And you don't nede to define Team as separate class, as i see it in your json it just a Dictionary<string, string>

Upvotes: 2

Related Questions