2015-07-06 58 views
0

我爲反序列化代碼這個JSONNull值

首先,我創建了一個類:

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; } 
    } 

所以我做了一個代碼解析請求,並返回a responseText

string responseText = Parser.Request(link); //Parser is the class that perform HttpRequest 

到目前爲止沒有問題。
我declarated爲反序列化對象的responseText返回:

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

和明年我做的foreach:

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

obj.fixture我得到空,我不知道爲什麼。因爲所有JSON都是正確反序列化的。我究竟做錯了什麼?

+0

你能減少你的例子,一個[小例子(http://stackoverflow.com /幫助/ MCVE)? – Seb

+0

使用自動生成類:http://json2csharp.com/ –

+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; } 
+0

不好意思,但是我對head2head對象有同樣的問題 –

+0

JSON根本沒有'head2head',也沒有任何看起來像它的東西。我建議你在json2csharp.com中重新導入JSON--在你進行第一次導入之後,提供方可能已經更改了JSON結構。 – Glorfindel