如何正确反序列化我的JSON对象

本文关键字:JSON 对象 我的 反序列化 何正确 | 更新日期: 2023-09-27 18:04:35

我有一个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. string]"。因为该类型需要一个JSON对象(例如>>{"name":"value"})来正确反序列化。

我发现这个错误是这个网站,但解决方案是改变这一行:

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

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

问题是我没有一个"对象"列表是我的JSON。那么该怎么做呢?

如何正确反序列化我的JSON对象

假设是JSON。NET知道将id放入字典的int key字段和stat属性中作为内部字典键…它可能不能推断出所有的……

其中一个类定义应该为您的JSON工作是:(我知道是驼色的而不是帕斯卡的…使用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/

您的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
}

添加一些类来表示您从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; }
}