Reputation: 441
I have a JSON file that I am trying to deserialize.
[
{
"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
}
]
}
]
}
]
This is the object I'm trying to deserialize it into:
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; }
}
And this is what I'm doing to load it from a file:
List<ImageBrainData_Reader> dataRead = JsonConvert.DeserializeObject<List<ImageBrainData_Reader>>(File.ReadAllText(fileName + ".json"));
The first part (colorData
) gets brought in and the correct amount of nested Neighbours
, but none of the data from them (Item1
and Item2
) are being read. Instead of getting the data, they are defaulting to their default values (0 and null, respectively).
Upvotes: 1
Views: 3099
Reputation: 129667
Json.Net doesn't have any way to know that Item1
maps to direction
and Item2
maps to neighbourData_Reader
unless you tell it. You either need to add some [JsonProperty]
attributes as shown below, or rename your properties to match the JSON.
public partial class Neighbour_Reader
{
[JsonProperty("Item1")]
public int direction { get; set; }
[JsonProperty("Item2")]
public List<NeighbourData_Reader> neighbourData_Reader { get; set; }
}
Fiddle: https://dotnetfiddle.net/ajE0HD
Upvotes: 2