I am working with an 3rd party API that returns JSON data. I then deserialize this into a C# class containing multiple objects.
The issue is that I sometimes receive an error explaining:
Cannot deserialize the current JSON array (e.g. [1,2,3]) into (my object type)
and that:
JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array
So, I decorate the object with an [JsonArray] attribute
However, I then get an error message explaining:
Cannot deserialize the current JSON object (e.g. {\"name\":\"value\"}) into (my object type)
and that:
JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object
It seems that sometimes the API is returning a JSON Array object and sometimes not.
The question is therefore how I define/decorate my object or define the class to be flexible enough to handle either? Is this possible?
This is the data that generates the 'Cannot deserialize the current JSON array' error:
"account_id": "0",
"account": [],
...
Here's the data that generates the 'object' error (when I decorate it as [JsonArray]):
"account_id": "3776",
"account": {
"id": 3776,
"name": "VISA Debit APP",
...
},
...
Obviously I want to nicely deserialize this to a C# class so I can work with data in a structured type safe manner. I have no control over the structure of this data so I'm compelled to work with this issue.
Any insights would be greatly appreciated.