【问题标题】:Newtonsoft JSON is not deserializing the nested object in my json fileNewtonsoft JSON 没有反序列化我的 json 文件中的嵌套对象
【发布时间】:2020-05-28 20:06:25
【问题描述】:

我有一个我正在尝试反序列化的 JSON 文件。

[
  {
    "colorData": [
      255,
      255,
      255
    ],
    "Neighbours": [
      {
        "Item1": 0,
        "Item2": [
          {
            "colorData": [
              255,
              255,
              255
            ],
            "numberOfExamples": 188
          },
          {
            "colorData": [
              255,
              24,
              0
            ],
            "numberOfExamples": 15
          }
        ]
      },
      {
        "Item1": 1,
        "Item2": [
          {
            "colorData": [
              255,
              255,
              255
            ],
            "numberOfExamples": 188
          },
          {
            "colorData": [
              255,
              24,
              0
            ],
            "numberOfExamples": 15
          }
        ]
      },
      {
        "Item1": 2,
        "Item2": [
          {
            "colorData": [
              255,
              255,
              255
            ],
            "numberOfExamples": 188
          },
          {
            "colorData": [
              255,
              24,
              0
            ],
            "numberOfExamples": 15
          }
        ]
      },
      {
        "Item1": 3,
        "Item2": [
          {
            "colorData": [
              255,
              255,
              255
            ],
            "numberOfExamples": 188
          },
          {
            "colorData": [
              255,
              24,
              0
            ],
            "numberOfExamples": 15
          }
        ]
      }
    ]
  }
]

这是我试图将其反序列化为的对象:

public partial class ImageBrainData_Reader
{
    public int[] colorData { get; set; }
    public List<Neighbour_Reader> neighbours { get; set; }
}

public partial class Neighbour_Reader
{
    public int direction { get; set; }
    public List<NeighbourData_Reader> neighbourData_Reader { get; set; }     
}

public partial class NeighbourData_Reader
{
    public int[] colorData { get; set; }
    public int numberOfExamples { get; set; }
}

这就是我从文件中加载它的方法:

 List<ImageBrainData_Reader> dataRead = JsonConvert.DeserializeObject<List<ImageBrainData_Reader>>(File.ReadAllText(fileName + ".json"));

第一部分 (colorData) 被引入并嵌套了正确数量的 Neighbours,但没有读取其中的任何数据(Item1Item2)。它们默认为默认值(分别为 0 和 null),而不是获取数据。

【问题讨论】:

    标签: c# json json.net deserialization


    【解决方案1】:

    Json.Net 无法知道 Item1 映射到 directionItem2 映射到 neighbourData_Reader 除非你告诉它。您需要添加一些 [JsonProperty] 属性,如下所示,或者重命名您的属性以匹配 JSON。

    public partial class Neighbour_Reader
    {
        [JsonProperty("Item1")]
        public int direction { get; set; }
        [JsonProperty("Item2")]
        public List<NeighbourData_Reader> neighbourData_Reader { get; set; }
    }
    

    小提琴:https://dotnetfiddle.net/ajE0HD

    【讨论】:

    • 天啊,我不敢相信这是导致问题的原因。我复制粘贴了从我的类中读入的对象,从该类中写入数据的对象是使用元组的(因此是 item1 和 item2)。非常感谢!我整天都被困在这上面!
    • 很高兴能帮上忙!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多