【问题标题】:C# Parse Json with multiple objects and arrays newtonsoftC# Parse Json 与多个对象和数组 newtonsoft
【发布时间】:2016-07-13 05:30:08
【问题描述】:

我试图在 C# 中使用 newtonsoft 解析相当复杂/不必要的复杂 JSON 输出,但是由于某种原因,我的解析器总是返回 null。我已经搜索了整个 SO,似乎找不到解决方案。

我尝试解析的 JSON 文件示例:

{
  "success": 1,
  "d": {
    "gameData": {
      "MJ2Y7tDg": {
        "scores": [
          {
            "max": 1.83,
            "avg": 1.73,
            "rest": 2,
            "active": true,
            "scoreid": "2c556xv464x0x4vtqc"
          },
          {
            "max": 3.47,
            "avg": 3.24,
            "rest": 2,
            "active": true,
            "scoreid": "2c556xv498x0x0"
          },
          {
            "max": 6.06,
            "avg": 5.08,
            "rest": 1,
            "active": true,
            "scoreid": "2c556xv464x0x4vtqd"
          }
        ],
        "count": 62,
        "highlight": [
          false,
          true
        ]
      },
      "jZICYUQu": {
        "scores": [
          {
            "max": 2.25,
            "avg": 2.13,
            "rest": null,
            "active": true,
            "scoreid": "2c5guxv464x0x4vuiv"
          },
          {
            "max": 3.55,
            "avg": 3.29,
            "rest": null,
            "active": true,
            "scoreid": "2c5guxv498x0x0"
          },
          {
            "max": 3.9,
            "avg": 3.33,
            "rest": null,
            "active": true,
            "scoreid": "2c5guxv464x0x4vuj0"
          }
        ],
        "count": 62,
        "highlight": [
          false,
          false
        ]
      }
    }
  }
}

这是我目前所拥有的,我对 JSON 争论很陌生 :)

public class RootObject
    {
        public int success { get; set; }
        public List<d> d { get; set; }
    }

    public class d
    {
        public List<gameData> gameData { get; set; }
    }

    public class gameData
    {
        public IDictionary<string, Score> gameData{ get; set; }
        public List<scores[]> GameList;
    }
    public class Score
    {
        public double max { get; set; }
        public double avg { get; set; }
        public int rest { get; set; }
        public bool active { get; set; }
        public string scoreid { get; set; }
    }

任何有更多 JSON 争论经验的人都知道如何让它工作?

谢谢你。 P.S 我现在在读高中,学习 C#

【问题讨论】:

  • 请出示您的代码
  • 他展示了他试图反序列化的类的代码,还有什么必要的?

标签: c# arrays json json.net


【解决方案1】:

解析器返回 null,因为您的类结构与 JSON 的结构不正确相似。正确的类结构是:

public class RootObject
{
    public int success { get; set; }
    public Class_d d { get; set; }
}

public class Class_d
{
    public Dictionary<string, GameData> gameData { get; set; }
}

public class GameData
{
    public List<Score> scores { get; set; }
    public int count { get; set; }
    public bool[] highlight { get; set; }
}

public class Score
{
    public decimal max { get; set; }
    public decimal avg { get; set; }
    public int? rest { get; set; }
    public bool active { get; set; }
    public string scoreid { get; set; }
}

你可以按如下方式使用它:

string json = "..."; // the JSON in your example
RootObject root = JsonConvert.DeserializeObject<RootObject>(json);

【讨论】:

  • 谢谢菲利克斯-b。还有一件事我将如何使用 console.writeline 转储数据。
  • 上面的代码不起作用。 Class_d 无法解析。游戏数据和分数很好。
猜你喜欢
  • 1970-01-01
  • 2017-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-20
  • 1970-01-01
  • 2020-09-06
  • 2019-04-26
相关资源
最近更新 更多