I have a json with structure like this:
{
"result":
[
{"a":{"b":1,"c":2}},
{"a":{"b":1,"c":2}},
{"a":{"b":1,"c":2}},
{"a":[]}
]
}
So I created class structure to parse this into C# objects:
public class JsonTest
{
public JsonTestResult[] result;
}
public class JsonTestResult
{
public JsonTestResultValue a;
}
public class JsonTestResultValue
{
public int b;
public int c;
}
when trying to parse this via LitJson
I get an error:
Type JsonTestResultValue can't act as an array
the problem is in this part of the json: {"a":[]}
, there must be {}
instead of []
, because it's really an object, not an array.
Now I'm stuck here and can't understand which type that I must use for this json property - array or any object type is unsuitable. All that I have on my mind now is to replace braces (with Regex of simple String.Replace
), but I'm sure, there must be more adequate way to deserialize this.
What you need to do is to create a custom converter and use inheritance.
To start with I've changed so that there are two subclasses:
Which are used in the main structure:
We then need to be able to identify which of the subclasses to use. To do that we can check if the start token is for an object or for an array. that is done inside a
JsonConverter
:Finally you have to specify the converter when deserializing:
You probably want to change the JArray against your real array type in the
JsonTestResultArray
.