2014-05-22 42 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; } 
}