Using C#, what is the correct way to deserialize a dynamic JSON object (ie: some JSON where the type of a single key can change between objects)?
For example, if I have this (perfectly valid) JSON:
{
"stuffs": [
{ "content": "abcd" },
{ "content": "efgh" },
{ "content": [
"ijkl",
"mnop"
]}
]
}
where thing
can be either a string or an array of strings, how do I deserialize this if I have these classes defined?
class ThingContainer {
List<Thing> stuffs;
}
class Thing {
List<string> content;
}
Attempting to deserialize I (expectedly) run into an exception:
Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[DummyProject.Thing]'