I am having a problem where the API handler I'm processing returns an OBJECT for an ARRAY of size 1.
For example, sometimes the API responds:
{
"monument": [
{
"key": 4152,
"name": "MTS - Corporate Head Office",
"categories": {},
"address": {}
},
{
"key": 4151,
"name": "Canadian Transportation Agency",
"categories": {},
"address": {}
},
{
"key": 4153,
"name": "Bank of Montreal Building",
"categories": {},
"address": {}
}
],
}
However, if the array monumenthas only 1 element, it becomes an OBJECT (note the absence of brackets []) like this:
{
"monument": {
"key": 4152,
"name": "MTS - Corporate Head Office",
"categories": {},
"address": {}
}
}
If I define my models as follows, I get an error when only one element is returned:
public class Locations {
public List<Monument> monument;
}
If only one element is returned, I get the following error:
Expected BEGIN_OBJECT but was BEGIN_ARRAY ...
And if I define my model as follows:
public class Locations {
public Monument monument;
}
and the API returns ARRAY. I get the opposite error.
Expected BEGIN_ARRAY but was BEGIN_OBJECT ...
I cannot define multiple elements with the same name in my model. How can I handle this case?
. API.