使用RestSharp检索JSON数据,传入的JSON如下所示...
{"data":{"NHL":[{"team_id":1,"team":"Colorado Avalanche","regular_season":{},"postseason":{}}]}}
我要序列化的C#对象看起来像这样。
public class NHLTeamStats
{
[DeserializeAs(Name = "data")]
public NHLTeamStatsData Data { get; set; }
}
public partial class NHLTeamStatsData
{
[DeserializeAs(Name = "NHL")]
public List<NHLTeamStatsNHL> NHL { get; set; }
}
public class NHLTeamStatsNHL
{
public NHLTeamStatsNHL()
{
RegularSeason = new NHLTeamStatsSeason();
Postseason = new NHLTeamStatsSeason();
}
[DeserializeAs(Name = "team_id")]
public int TeamId { get; set; }
[DeserializeAs(Name = "team")]
public string Team { get; set; }
[DeserializeAs(Name = "regular_season")]
public NHLTeamStatsSeason RegularSeason { get; set; }
[DeserializeAs(Name = "postseason")]
public NHLTeamStatsSeason Postseason { get; set; }
}
public class NHLTeamStatsSeason
{
[DeserializeAs(Name = "hits")]
public int? Hits { get; set; }
[DeserializeAs(Name = "goals")]
public int? Goals { get; set; }
[DeserializeAs(Name = "saves")]
public int? Saves { get; set; }
[DeserializeAs(Name = "assists")]
public int? Assists { get; set; }
[DeserializeAs(Name = "power_plays")]
public int? PowerPlays { get; set; }
[DeserializeAs(Name = "faceoffs_won")]
public int? FaceoffsWon { get; set; }
[DeserializeAs(Name = "games_played")]
public int? GamesPlayed { get; set; }
[DeserializeAs(Name = "faceoffs_lost")]
public int? FaceoffsLost { get; set; }
[DeserializeAs(Name = "shots_on_goal")]
public int? ShotsOnGoal { get; set; }
[DeserializeAs(Name = "penalty_minutes")]
public int? PenaltyMinutes { get; set; }
[DeserializeAs(Name = "power_plays_converted")]
public int? PowerPlaysConverted { get; set; }
}
当我执行RestSharp Execute调用时,响应为空,如您所见in this screenshot。如果我在没有指定类型的情况下执行Execute方法,响应会很好。仅当属性regular_season,postseason设置为空对象时才会发生 “ regular_season”:{},“ postseason”:{}
当他们有数据时,它可以正常工作并进行序列化。
有什么想法吗?