Reputation: 3
I'm trying to deserialize different object types inside a json array in the most efficient way possible but I can't really see how this can be done in any sort of straightforward way. I'm using the Newtonsoft.Json library for parsing.
I have set up a simple example to illustrate the problem I'm trying to solve, the classes I wish to bind to and the JSON format I'm working with is defined below:
class Car
{
public int Wheels { get; set; }
public String Manufacturer { get; set; }
public String Colour { get; set; }
public double EngineSize { get; set; }
public String Year { get; set; }
}
class Aeroplane
{
public String Model { get; set; }
public String Airline { get; set; }
public int JourneyCount { get; set; }
}
class Ship
{
public String Name { get; set; }
public String Company { get; set; }
public int Capacity { get; set; }
public String YearBuilt { get; set; }
}
And here's the JSON I'm trying to bind to these classes. It's set up as an array with three different nested object types at each position. Basically each element/index in the array contains three objects, namely a car, aeroplane and ship.
{[
{
"car": {
"wheels": 4,
"manufacturer": BMW,
"colour": "Black",
"engineSize": "1.6"
},
"aeroplane": null,
"ship": null
},
{
"car": {
"wheels": 4,
"manufacturer": BMW,
"colour": "Black",
"engineSize": "1.6"
},
"aeroplane": {
"model": "Dreamliner",
"airline": "Emirates",
"journeyCount": 4798
},
"ship": {
"name":"Queen Mary",
"company":"Cunard",
"capacity": 890,
"yearBuilt": 2000
}
},
{
"car": {
"wheels": 4,
"manufacturer": BMW,
"colour": "Black",
"engineSize": "1.6"
},
"aeroplane": {
"model": "Boeing 777",
"airline": "BA",
"journeyCount": 6158
},
"ship": {
"name":"HMS Diamond",
"company":"Royal Navy",
"capacity": 500,
"yearBuilt": 2010
}
}
]}
Upvotes: 0
Views: 1197
Reputation: 5299
First your json is not valid. To make it valid you need to get rid of the top curly braces. Also there are some missing quotes. You can use JSON Lint to validate it. It should look like this:
[
{
"car": {
"wheels": 4,
"manufacturer": "BMW",
"colour": "Black",
"engineSize": "1.6"
},
"aeroplane": null,
"ship": null
},
{
"car": {
"wheels": 4,
"manufacturer": "BMW",
"colour": "Black",
"engineSize": "1.6"
},
"aeroplane": {
"model": "Dreamliner",
"airline": "Emirates",
"journeyCount": 4798
},
"ship": {
"name": "Queen Mary",
"company": "Cunard",
"capacity": 890,
"yearBuilt": 2000
}
},
{
"car": {
"wheels": 4,
"manufacturer": "BMW",
"colour": "Black",
"engineSize": "1.6"
},
"aeroplane": {
"model": "Boeing 777",
"airline": "BA",
"journeyCount": 6158
},
"ship": {
"name": "HMS Diamond",
"company": "Royal Navy",
"capacity": 500,
"yearBuilt": 2010
}
}
]
You need one more class to wrap car, aeroplane and ship:
public class VehicleWrapper
{
public Car Car { get; set; }
public Aeroplane Aeroplane { get; set; }
public Ship ship { get; set; }
}
Now you can deserialize it with the following line:
List<VehicleWrapper> vehicles = JsonConvert.DeserializeObject<List<VehicleWrapper>>(jsonString);
Upvotes: 4