2014-05-22 83 views
0

我有一個JSON反序列化的問題。我創建了一個C#程序,並使用了Json.NET。如何正確反序列化我的JSON對象?

JSON對象,我必須反序列化是:

{ 
    "modifyDate": 1400648078000, 
    "champions": [ 
     { 
     "id": 143, 
    "stats": { 
     "totalDeathsPerSession": 7, 
     "totalSessionsPlayed": 1, 
     "totalDamageTaken": 19377, 
     "totalQuadraKills": 0, 
     "totalTripleKills": 0, 
     "totalMinionKills": 38, 
     "maxChampionsKilled": 4, 
     "totalDoubleKills": 0, 
     "totalPhysicalDamageDealt": 4862, 
     "totalChampionKills": 4, 
     "totalAssists": 17, 
     "mostChampionKillsPerSession": 4, 
     "totalDamageDealt": 53289, 
     "totalFirstBlood": 0, 
     "totalSessionsLost": 0, 
     "totalSessionsWon": 1, 
     "totalMagicDamageDealt": 46696, 
     "totalGoldEarned": 12787, 
     "totalPentaKills": 0, 
     "totalTurretsKilled": 0, 
     "mostSpellsCast": 0, 
     "maxNumDeaths": 7, 
     "totalUnrealKills": 0 
    } 
    }, 
    { 
    "id": 115, 
    "stats": { 
     "totalDeathsPerSession": 8, 
     "totalSessionsPlayed": 1, 
     "totalDamageTaken": 18926, 
     "totalQuadraKills": 0, 
     "totalTripleKills": 0, 
     "totalMinionKills": 219, 
     "maxChampionsKilled": 4, 
     "totalDoubleKills": 0, 
     "totalPhysicalDamageDealt": 8912, 
     "totalChampionKills": 4, 
     "totalAssists": 6, 
     "mostChampionKillsPerSession": 4, 
     "totalDamageDealt": 170050, 
     "totalFirstBlood": 0, 
     "totalSessionsLost": 1, 
     "totalSessionsWon": 0, 
     "totalMagicDamageDealt": 161137, 
     "totalGoldEarned": 10950, 
     "totalPentaKills": 0, 
     "totalTurretsKilled": 0, 
     "mostSpellsCast": 0, 
     "maxNumDeaths": 8, 
     "totalUnrealKills": 0 
    } 
    }, ... 

這是一個複雜的對象。

代表這個對象我的C#類是:

class StatRankedJoueur 
{ 

    private double modifyDate; 
    public double ModifyDate 
    { 
     get { return modifyDate; } 
     set { modifyDate = value; } 
    } 


    private int _summonerId; 
    public int SummonerId 
    { 
     get { return _summonerId; } 
     set { _summonerId = value; } 
    } 

    private Dictionary<int, Dictionary<String, double>> champions; 
    public Dictionary<int, Dictionary<String, double>> Champions 
    { 
     get 
     { 
      return champions; 
     } 

     set 
     { 

      champions = value; 
     } 
    } 
} 

反序列化:

StatRankedJoueur values = JsonConvert.DeserializeObject<StatRankedJoueur>(response); 

但我已經這個問題,當我編譯:

無法反序列化當前JSON數組(例如[1,2,3])類型爲'System.Collections.Generic.Dictionary 2[System.Int32,System.Collections.Generic.Dictiona>y 2 [System.String,System.Double]]'b因爲該類型需要JSON對象(例如, >> {「name」:「value」})正確反序列化。

我發現這個錯誤是這樣的網站,但解決辦法是改變這一行:

StatRankedJoueur values = JsonConvert.DeserializeObject<StatRankedJoueur>(response); 

由:

var list = JsonConvert.DeserializeObject<List<KeyValuePair<string,List<KeyValuePair<string, string>>>> >(json); 

問題是我沒有「對象「列表是我的JSON。那麼怎麼做呢?

+0

附註:避免新的在這裏/謝謝你在筆記和標題標題。顯示問題信息/代碼(正如你所做的)綽綽有餘。 –

+1

請注意,在上面給出的JSON示例中,「冠軍」是**數組**,而在您的C#類中則是**字典**。因此,JSON解串器正確地抱怨這種不匹配。一種方法是創建*另一個類*,用JSON對象與「id」和「stats」屬性匹配,並聲明你的*冠軍*屬性,如'Public List 冠軍' – elgonzo

回答

0

你假設JSON.NET知道把一個id爲您的字典和統計性的INT鍵字段作爲內部字典鍵......它可能不能夠推斷出所有...

一類的定義應該爲你的JSON工作的是: (我知道這是駱駝套管代替Pascal大小寫...更改,使用JSON屬性)

public class Stats 
{ 
    public int totalDeathsPerSession { get; set; } 
    public int totalSessionsPlayed { get; set; } 
    public int totalDamageTaken { get; set; } 
    public int totalQuadraKills { get; set; } 
    public int totalTripleKills { get; set; } 
    public int totalMinionKills { get; set; } 
    public int maxChampionsKilled { get; set; } 
    public int totalDoubleKills { get; set; } 
    public int totalPhysicalDamageDealt { get; set; } 
    public int totalChampionKills { get; set; } 
    public int totalAssists { get; set; } 
    public int mostChampionKillsPerSession { get; set; } 
    public int totalDamageDealt { get; set; } 
    public int totalFirstBlood { get; set; } 
    public int totalSessionsLost { get; set; } 
    public int totalSessionsWon { get; set; } 
    public int totalMagicDamageDealt { get; set; } 
    public int totalGoldEarned { get; set; } 
    public int totalPentaKills { get; set; } 
    public int totalTurretsKilled { get; set; } 
    public int mostSpellsCast { get; set; } 
    public int maxNumDeaths { get; set; } 
    public int totalUnrealKills { get; set; } 
} 

public class Champion 
{ 
    public int id { get; set; } 
    public Stats stats { get; set; } 
} 

public class RootObject 
{ 
    public long modifyDate { get; set; } 
    public List<Champion> champions { get; set; } 
} 

你可以在這裏爲您的JSON快速生成C#類:http://json2csharp.com/

+0

Ty, 但是我應該改變此行現在: JsonClass.StatRankedJoueur values = JsonConvert.DeserializeObject (效應初探); 由於summonerId已更改,但冠軍列表未更改 – merinid

0

您的JSON不包含字典,正如評論指出的那樣。試試這個:

class StatRankedJoueur 
{ 

    private double modifyDate; 
    public double ModifyDate 
    { 
     get { return modifyDate; } 
     set { modifyDate = value; } 
    } 


    private int _summonerId; 
    public int SummonerId 
    { 
     get { return _summonerId; } 
     set { _summonerId = value; } 
    } 

    private Champion[] champions; 
    public Champion[] Champions 
    { 
     get 
     { 
      return champions; 
     } 

     set 
     { 

      champions = value; 
     } 
    } 


} 

public class Champion 
{ 
    public int id { get; set; } 
    public Stats stats { get; set; } 
} 

public class Stats 
{ 
    public int totalDeathsPerSession { get; set; } 
    //etc 
} 
0

添加幾個類來表示從JSON獲得的結構。

public class StatRankedJoueur 
{ 
    public double ModifyDate { get; set; } 
    public int SummonerId { get; set; } 
    public IEnumerable<Champion> Champions { get; set; } 
} 

public class Champion 
{ 
    public int id { get; set;} 
    public Stats stats { get; set;} 
} 

public class Stats 
{ 
    public double totalDeathsPerSession { get; set; } 
    public double totalSessionsPlayed { get; set; } 
    public double totalDamageTaken { get; set; } 
    public double totalQuadraKills { get; set; } 
    public double totalTripleKills { get; set; }, 
    public double totalMinionKills { get; set; } 
    public double maxChampionsKilled { get; set; } 
    public double totalDoubleKills { get; set; } 
    public double totalPhysicalDamageDealt { get; set; } 
    public double totalChampionKills { get; set; } 
    public double totalAssists { get; set; } 
    public double mostChampionKillsPerSession { get; set; } 
    public double totalDamageDealt { get; set; } 
    public double totalFirstBlood { get; set; } 
    public double totalSessionsLost { get; set; } 
    public double totalSessionsWon { get; set; } 
    public double totalMagicDamageDealt { get; set; } 
    public double totalGoldEarned { get; set; } 
    public double totalPentaKills { get; set; } 
    public double totalTurretsKilled { get; set; } 
    public double mostSpellsCast { get; set; } 
    public double maxNumDeaths { get; set; } 
    public double totalUnrealKills { get; set; } 
}