JSON 对象为空
JSON Object Null
我成功反序列化了 JSON 文件,我得到了结果,但在结果结束后,我得到了“对象未设置为对象的实例”错误
JSON 文件:
"radiant_team": {
"team_name": "EHOME",
"team_id": 4,
"team_logo": 52122954231169668,
"complete": true
},
"dire_team": {
"team_name": "Team Secret",
"team_id": 1838315,
"team_logo": 543025270456493033,
"complete": true
public partial class LiveLeagues
{
public LiveGames Result { get; set; }
}
public class LiveGames
{
public List<GameStats> games { get; set; }
public int status { get; set; }
}
public class GameStats
{
public List<BasePlayer> players { get; set; }
public RadiantTeam radiant_team { get; set; }
public DireTeam dire_team { get; set; }
}
public class DireTeam
{
public string team_name { get; set; }
public int team_id { get; set; }
public object team_logo { get; set; }
public bool complete { get; set; }
}
public class RadiantTeam
{
public string team_name { get; set; }
public int team_id { get; set; }
public object team_logo { get; set; }
public bool complete { get; set; }
}
LiveLeagues.LiveLeagues liveGames = JsonConvert.DeserializeObject<LiveLeagues.LiveLeagues>(response.Content.ReadAsStringAsync().Result);
foreach (var leagues in liveGames.Result.games)
{
MessageBox.Show(leagues.dire_team.team_id.ToString());
MessageBox.Show(leagues.radiant_team.team_id.ToString());
}
我尝试遍历 JSON 并测试这些值是否会显示。我在 MessageBox.Show 上测试了它,得到了结果 "EHOME" 和 "Team Secret" 但之后出现错误“对象未设置为对象的实例”
当 json 允许没有 dire_team and/or radiant_team 属性 的游戏时,您应该进行空值检查以确保它们存在:
foreach (var leagues in liveGames.Result.games){
if(leagues.dire_team != null)
MessageBox.Show(leagues.dire_team.team_id.ToString());
else
MessageBox.Show("no dire team for this game");
if(leagues.radiant_team != null)
MessageBox.Show(leagues.radiant_team.team_id.ToString());
else
MessageBox.Show("no radiant team for this game");
}
或者您可以尝试在 GameStats 的构造函数中为这些对象使用默认值。
public class GameStats
{
public List<BasePlayer> players { get; set; }
public RadiantTeam radiant_team { get; set; }
public DireTeam dire_team { get; set; }
public GameStats(){
dire_team = new DireTeam();
radiant_team = new RadiantTeam();
}
}
我成功反序列化了 JSON 文件,我得到了结果,但在结果结束后,我得到了“对象未设置为对象的实例”错误
JSON 文件:
"radiant_team": {
"team_name": "EHOME",
"team_id": 4,
"team_logo": 52122954231169668,
"complete": true
},
"dire_team": {
"team_name": "Team Secret",
"team_id": 1838315,
"team_logo": 543025270456493033,
"complete": true
public partial class LiveLeagues
{
public LiveGames Result { get; set; }
}
public class LiveGames
{
public List<GameStats> games { get; set; }
public int status { get; set; }
}
public class GameStats
{
public List<BasePlayer> players { get; set; }
public RadiantTeam radiant_team { get; set; }
public DireTeam dire_team { get; set; }
}
public class DireTeam
{
public string team_name { get; set; }
public int team_id { get; set; }
public object team_logo { get; set; }
public bool complete { get; set; }
}
public class RadiantTeam
{
public string team_name { get; set; }
public int team_id { get; set; }
public object team_logo { get; set; }
public bool complete { get; set; }
}
LiveLeagues.LiveLeagues liveGames = JsonConvert.DeserializeObject<LiveLeagues.LiveLeagues>(response.Content.ReadAsStringAsync().Result);
foreach (var leagues in liveGames.Result.games)
{
MessageBox.Show(leagues.dire_team.team_id.ToString());
MessageBox.Show(leagues.radiant_team.team_id.ToString());
}
我尝试遍历 JSON 并测试这些值是否会显示。我在 MessageBox.Show 上测试了它,得到了结果 "EHOME" 和 "Team Secret" 但之后出现错误“对象未设置为对象的实例”
当 json 允许没有 dire_team and/or radiant_team 属性 的游戏时,您应该进行空值检查以确保它们存在:
foreach (var leagues in liveGames.Result.games){
if(leagues.dire_team != null)
MessageBox.Show(leagues.dire_team.team_id.ToString());
else
MessageBox.Show("no dire team for this game");
if(leagues.radiant_team != null)
MessageBox.Show(leagues.radiant_team.team_id.ToString());
else
MessageBox.Show("no radiant team for this game");
}
或者您可以尝试在 GameStats 的构造函数中为这些对象使用默认值。
public class GameStats
{
public List<BasePlayer> players { get; set; }
public RadiantTeam radiant_team { get; set; }
public DireTeam dire_team { get; set; }
public GameStats(){
dire_team = new DireTeam();
radiant_team = new RadiantTeam();
}
}