我遇到了JSON反序列化的问题。我创建了一个C#程序,并使用了Json.NET。
我必须反序列化的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]]',因为类型需要要正确反序列化的JSON对象(例如&gt;&gt; {“name”:“value”})。
我发现这个错误是这个网站,但解决方法是改变这一行:
StatRankedJoueur values = JsonConvert.DeserializeObject<StatRankedJoueur>(response);
由:
var list = JsonConvert.DeserializeObject<List<KeyValuePair<string,List<KeyValuePair<string, string>>>> >(json);
问题是我没有“对象”列表是我的JSON。那怎么办呢?
答案 0 :(得分: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
}
答案 1 :(得分:0)
你假设JSON.NET知道将id放入你的字典的int key字段和stat属性作为内部字典键...它可能无法推断所有...
适用于您的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/
答案 2 :(得分: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; }
}