[英]Deserialize JSON with dynamic properties with JSON.NET in C#
I've got a JSON that, for example, looks like 我有一个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":{}
}
As you can see there are dynamic properties and field in match and in team. 如您所见,比赛和团队中都有动态属性和字段。 I tried to reflect this in my c# classes 我试图在我的C#类中反映出这一点
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. 它被反序列化,没有错误,但是之后的HistoryMatch
类的Attributes Match
和Team
都为null
。 I've taken several attemps now but it seems that I'm missing something essential here. 我现在尝试了几次,但似乎这里缺少一些必要的东西。 Could someone please give me an advise? 有人可以给我一个建议吗?
In your Json Team
, Match
and Notes
are not parts of the HistoryMatch
, but parts of the History
. 在您的Json Team
,“ Match
和“ Notes
不是HistoryMatch
,而是History
。 Define your History
class this way, i did it an i have deserialized your team and match values. 用这种方式定义您的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; }
}
And you don't nede to define Team
as separate class, as i see it in your json it just a Dictionary<string, string>
而且您不必将Team
定义为单独的类,正如我在json中看到的那样,它只是Dictionary<string, string>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.