使用C#中的JSON.NET使用动态属性反序列化JSON

时间:2017-01-14 20:35:44

标签: c# json json.net json-deserialization

我有一个JSON,例如,看起来像

Start with two coprime branches: (2,1), (3,1), then iterate:
Branch 1: (2m-n,m)
Branch 2: (2m+n,m)
Branch 3: (m+2n,n)

正如您所看到的,匹配和团队中都有动态属性和字段。 我试图在我的c#classes

中反映这一点
{"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":{}
}

然后我按照以下方式反序列化

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
{

}

反序列化时没有错误,但之后var obj = JsonConvert.DeserializeObject<History<Match>>(responseText); MatchTeam的属性为HistoryMatch。 我现在已经进行了几次尝试,但似乎我在这里缺少必要的东西。 有人可以给我一个建议吗?

1 个答案:

答案 0 :(得分:2)

在你的Json Team中,MatchNotes不是HistoryMatch的一部分,而是History的一部分。以这种方式定义您的History课程,我做了一个我已经反序列化您的团队并匹配值。

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

并且您不需要将Team定义为单独的类,因为我在您的json中看到它只是Dictionary<string, string>