json对象中的空值

时间:2015-07-06 08:50:37

标签: c# json wpf json.net

我制作了一个用于反序列化此JSON

的代码

首先,我创建了一个类:

public class Self
    {
        public string href { get; set; }
    }

    public class Soccerseason
    {
        public string href { get; set; }
    }

    public class HomeTeam
    {
        public string href { get; set; }
    }

    public class AwayTeam
    {
        public string href { get; set; }
    }

    public class Links
    {
        public Self self { get; set; }
        public Soccerseason soccerseason { get; set; }
        public HomeTeam homeTeam { get; set; }
        public AwayTeam awayTeam { get; set; }
    }

    public class Result
    {
        public int goalsHomeTeam { get; set; }
        public int goalsAwayTeam { get; set; }
    }

    public class LastHomeWinHomeTeam
    {
        public Links _links { get; set; }
        public string date { get; set; }
        public object status { get; set; }
        public int matchday { get; set; }
        public string homeTeamName { get; set; }
        public string awayTeamName { get; set; }
        public Result result { get; set; }
    }

    public class LastWinHomeTeam
    {
        public Links _links { get; set; }
        public string date { get; set; }
        public object status { get; set; }
        public int matchday { get; set; }
        public string homeTeamName { get; set; }
        public string awayTeamName { get; set; }
        public Result result { get; set; }
    }

    public class LastAwayWinAwayTeam
    {
        public Links _links { get; set; }
        public string date { get; set; }
        public object status { get; set; }
        public int matchday { get; set; }
        public string homeTeamName { get; set; }
        public string awayTeamName { get; set; }
        public Result result { get; set; }
    }

    public class LastWinAwayTeam
    {
        public Links _links { get; set; }
        public string date { get; set; }
        public object status { get; set; }
        public int matchday { get; set; }
        public string homeTeamName { get; set; }
        public string awayTeamName { get; set; }
        public Result result { get; set; }
    }

    public class Head2head
    {
        public int count { get; set; }
        public string timeFrameStart { get; set; }
        public string timeFrameEnd { get; set; }
        public int homeTeamWins { get; set; }
        public int awayTeamWins { get; set; }
        public int draws { get; set; }
        public LastHomeWinHomeTeam lastHomeWinHomeTeam { get; set; }
        public LastWinHomeTeam lastWinHomeTeam { get; set; }
        public LastAwayWinAwayTeam lastAwayWinAwayTeam { get; set; }
        public LastWinAwayTeam lastWinAwayTeam { get; set; }
        public List<Fixture> fixtures { get; set; }
    }

    public class Fixture
    {
        public Links _links { get; set; }
        public string date { get; set; }
        public object status { get; set; }
        public int matchday { get; set; }
        public string homeTeamName { get; set; }
        public string awayTeamName { get; set; }
        public Result result { get; set; }
    }

    public class RootObject
    {
        public List<Fixture> fixture { get; set; }
        public Head2head head2head { get; set; }
    }

所以我创建了一个解析请求的代码,然后返回responseText

string responseText = Parser.Request(link); //Parser is the class that perform HttpRequest
到目前为止没问题。
我已经声明了用于反序列化返回的responseText的对象:

 var obj = JsonConvert.DeserializeObject<Fixtures.RootObject>(responseText);

接下来我做了foreach:

foreach (var fixture in obj.fixture)
{do stuff..}

但是在obj.fixture我得到了空,我不知道为什么。因为所有JSON都是相应的反序列化。我做错了什么?

1 个答案:

答案 0 :(得分:4)

JSON中的密钥是"fixtures" - 它需要与您的类的属性名完全匹配。变化

public List<Fixture> fixture { get; set; }

public List<Fixture> fixtures { get; set; }

或者,您可以使用JsonProperty属性:

[JsonProperty("fixtures")]
public List<Fixture> fixture { get; set; }