C# 使用多个对象和数组解析 Json 牛顿软件



我正在尝试在 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#

解析器返回 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);

相关内容

  • 没有找到相关文章

最新更新