我遇到的问题是我正在解析的API为大小为1的ARRAY返回一个OBJECT.
例如,有时API会响应:
{
"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": {}
}
],
}
Run Code Online (Sandbox Code Playgroud)
但是,如果monument数组只有一个项目,它就变成了一个OBJECT(注意缺少[]括号),如下所示:
{
"monument": {
"key": 4152,
"name": "MTS - Corporate Head Office",
"categories": {},
"address": {}
}
}
Run Code Online (Sandbox Code Playgroud)
如果我像这样定义我的模型,只返回一个项目时会出错:
public class Locations {
public List<Monument> monument;
}
Run Code Online (Sandbox Code Playgroud)
如果只返回一个项目,我会收到以下错误:
Expected BEGIN_OBJECT …Run Code Online (Sandbox Code Playgroud)