在 C# 中使用 JSON.NET 反序列化具有动态属性的 JSON
Deserialize JSON with dynamic properties with JSON.NET in C#
我有一个 JSON,例如,看起来像
{"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":{}
}
如您所见,比赛和团队中都有动态属性和字段。
我试图在我的 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
{
}
然后我反序列化如下
var obj = JsonConvert.DeserializeObject<History<Match>>(responseText);
反序列化没有错误,但是 class HistoryMatch
的属性 Match
和 Team
之后是 null
。
我现在已经尝试了几次,但似乎我在这里遗漏了一些重要的东西。
有人可以给我建议吗?
在您的 Json Team
中,Match
和 Notes
不是 HistoryMatch
的一部分,而是 History
的一部分。以这种方式定义你的 History
class,我做到了,我已经反序列化你的团队并匹配值。
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
定义为单独的 class,正如我在你的 json 中看到的那样,它只是一个 Dictionary<string, string>
我有一个 JSON,例如,看起来像
{"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":{}
}
如您所见,比赛和团队中都有动态属性和字段。 我试图在我的 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
{
}
然后我反序列化如下
var obj = JsonConvert.DeserializeObject<History<Match>>(responseText);
反序列化没有错误,但是 class HistoryMatch
的属性 Match
和 Team
之后是 null
。
我现在已经尝试了几次,但似乎我在这里遗漏了一些重要的东西。
有人可以给我建议吗?
在您的 Json Team
中,Match
和 Notes
不是 HistoryMatch
的一部分,而是 History
的一部分。以这种方式定义你的 History
class,我做到了,我已经反序列化你的团队并匹配值。
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
定义为单独的 class,正如我在你的 json 中看到的那样,它只是一个 Dictionary<string, string>