C# Newtonsoft JSON未反序列化我的JSON文件中的嵌套对象
我有一个JSON文件,我正在尝试反序列化C# Newtonsoft JSON未反序列化我的JSON文件中的嵌套对象,c#,json,json.net,deserialization,C#,Json,Json.net,Deserialization,我有一个JSON文件,我正在尝试反序列化 [ { "colorData": [ 255, 255, 255 ], "Neighbours": [ { "Item1": 0, "Item2": [ { "colorData": [ 255, 255, 255
[
{
"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; }
}
公共部分类ImageBrainData\u读取器
{
public int[]colorData{get;set;}
公共列表邻居{get;set;}
}
公共部分类读卡器
{
公共int方向{get;set;}
公共列表邻居数据读取器{get;set;}
}
公共部分类邻域数据读取器
{
public int[]colorData{get;set;}
public int numberOfExamples{get;set;}
}
这就是我从文件中加载它所做的:
List<ImageBrainData_Reader> dataRead = JsonConvert.DeserializeObject<List<ImageBrainData_Reader>>(File.ReadAllText(fileName + ".json"));
List dataRead=JsonConvert.DeserializeObject(File.ReadAllText(fileName+“.json”);
第一部分(
colorData
)被引入,嵌套的邻居的正确数量
,但是没有从它们(Item1
和Item2
)读取任何数据。它们没有获取数据,而是默认为默认值(分别为0和null)。Json.Net无法知道Item1
映射到direction
,而Item2
映射到neigurdata\u 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; }
}
公共部分类邻居\u读取器
{
[JsonProperty(“第1项”)]
公共int方向{get;set;}
[JsonProperty(“第2项”)]
公共列表邻居数据读取器{get;set;}
}
小提琴手:天哪,我不敢相信这就是问题的根源。我复制粘贴了从我的类中读取到的对象,这些类使用Tuple(因此是item1和item2)写入数据。非常感谢你!我一整天都在忙这个!很高兴我能帮忙!