Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何正确地反序列化我的JSON对象?_C#_.net_Json_Serialization_Json.net - Fatal编程技术网

C# 如何正确地反序列化我的JSON对象?

C# 如何正确地反序列化我的JSON对象?,c#,.net,json,serialization,json.net,C#,.net,Json,Serialization,Json.net,我对JSON反序列化有问题。我创建了一个C#程序,并使用Json.NET 我必须反序列化的JSON对象是: { "modifyDate": 1400648078000, "champions": [ { "id": 143, "stats": { "totalDeathsPerSession": 7, "totalSessionsPlayed": 1, "totalDamageTaken": 19377

我对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;
        }
    }
}
作者:

var list=JsonConvert.DeserializeObject(json);

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

正如一条评论所指出的,您的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.NET知道将id作为内部字典键放入字典的int key字段和stat属性中。。。它可能无法推断所有这些

适用于JSON的类定义之一是: (我知道它是驼峰大小写而不是pascal大小写…使用JSON属性更改。)

公共类统计信息
{
公共int TotalDeath会话{get;set;}
公共int TotalSessionPlayed{get;set;}
公共int TotalDamageTake{get;set;}
公共整数totalQuadraKills{get;set;}
公共整数totalTripleKills{get;set;}
公共int totalMinionKills{get;set;}
public int maxChampionsKilled{get;set;}
公共整数{get;set;}
公共int TotalPhysicalDamageDemanted{get;set;}
公共int totalChampionKills{get;set;}
公共整数{get;set;}
公共int-mostChampionKillsPerSession{get;set;}
公共int TotalDamageDealed{get;set;}
公共整数totalFirstBlood{get;set;}
公共int TotalSessionLost{get;set;}
公共int totalSessionsWon{get;set;}
公共整数TotalMagicDamageDemand{get;set;}
公共整数{get;set;}
公共整数{get;set;}
公共整数{get;set;}
公共int mostSpellsCast{get;set;}
public int maxNumDeath{get;set;}
公共int totalUnrealKills{get;set;}
}
公开课冠军
{
公共int id{get;set;}
公共统计数据{get;set;}
}
公共类根对象
{
公共长修改日期{get;set;}
公共列表{get;set;}
}

您可以在此处为JSON快速生成C#类:

添加几个类来表示您从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; }
}
statrankedjouer公共类
{
公共双修改日期{get;set;}
public int calleerid{get;set;}
公共IEnumerable Champions{get;set;}
}
公开课冠军
{
公共int id{get;set;}
公共统计数据{get;set;}
}
公共类统计
{
公共双TotalDeath会话{get;set;}
公共双TotalSessionPlayed{get;set;}
公共双TotalDamageTake{get;set;}
公共双totalQuadraKills{get;set;}
公共双totalTripleKills{get;set;},
公共双totalMinionKills{get;set;}
公共双maxChampionsKilled{get;set;}
公共双totalDoubleKills{get;set;}
公共双TotalPhysicalDamageProcessed{get;set;}
公共双totalChampionKills{get;set;}
公共双精度{get;set;}
公共双mostChampionKillsPerSession{get;set;}
公共双TotalDamageDeposed{get;set;}
公共双totalFirstBlood{get;set;}
公共双TotalSessionLost{get;set;}
公共双totalSessionsWon{get;set;}
公共双TotalMagicDamageDemand{get;set;}
公共双总计{get;set;}
公共双精度{get;set;}
公共双精度{get;set;}
公共双mostSpellsCast{get;set;}
公共双MaxNumDeath{get;set;}
公共双totalUnrealKills{get;set;}
}

旁注:避免在帖子中添加新的/谢谢的注释,并在标题中添加标签。显示问题信息/代码(如您所做的)就足够了。请注意,在上面给出的JSON示例中,“champions”是一个对象数组,而在C#类中它是一个字典。因此,JSON反序列化程序正确地抱怨这种不匹配。一种方法是创建另一个类,该类将JSON对象与“id”和“stats”属性相匹配,并声明您的Champions属性,如
public List Champions
Ty,但我现在是否应该更改这一行:JsonClass.statrankedjouer values=JsonConvert.DeserializeObject(reponse);因为召唤者身份已经改变,但冠军名单没有改变
var list = JsonConvert.DeserializeObject<List<KeyValuePair<string,List<KeyValuePair<string, string>>>> >(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
}
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; }
}
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; }
}