I would like to deserialize a JSON string which contains multiple object types. So for instance here are my two POCO classes:
public class Car
{
[Key]
public int Id { get; set; }
public string Color { get; set; }
}
public class Driver
{
[Key]
public int Id { get; set; }
public string Firstname { get; set; }
}
Now I would like to have a JSON string which would have an array of objects from both classes (which are unrelated) and that would get deserialized into their respective POCO classes using the Newtonsoft json package in Visual Studio.
Is this possible and if so, what does the JSON format look like? An example would be appreciated with just 2 objects per class to show the array of objects and the two class types coexisting within a single JSON string which is passed to something like JsonConvert.DeserializeObject.
EDIT: the JSON for these classes and an object within would individually look something like:
for Car...
[
{
"color": "red"
},
{
"color": "blue"
}
]
and for Driver...
[
{
"Firstname": "Fred"
},
{
"Firstname": "Sally"
}
]
But now could those be combined into one string, but with some sort of extra parm to define to which CLASS each set belongs such as:
"Car"
[
{
"color": "red"
},
{
"color": "blue"
}
],
"Driver"
[
{
"Firstname": "Fred"
},
{
"Firstname": "Sally"
}
]
See how this has 2 unrelated classes in a single JSON string? Now I'd like to do a single (if possible) Jsonconvert into the respective classes so that "Car" winds up with 2 objects and "Driver" winds up with 2 (for this example).