我试图从API中获取一些项目,如下所示:
{ "picklist": [
{ "name": "Abkhazia", "id": "a0511000002gxF1AAI" },
{ "name": "Afghanistan", "id": "a0511000002gxF2AAI" },
{ "name": "Akrotiri and Dhekelia", "id": "a0511000002gxF3AAI" },
{ "name": "Albania", "id": "a0511000002gxF5AAI" },
{ "name": "Algeria", "id": "a0511000002gxF6AAI" },
{ "name": "American Samoa", "id": "a0511000002gxF7AAI" }
] }
我尝试过这样取道:
public interface MyAPI {
@GET("/?country=true")
Call<List<User>> getUsers();
}
但是因为这个表有一个名字会引发异常(至少我认为这就是原因):
java.lang.IllegalStateException:
Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 3 path $
我该如何解决?
答案 0 :(得分:2)
您应该针对此案例使用自定义响应:
public interface MyAPI {
@GET("/?country=true")
Call<APIResponse> getUsers();
}
class APIResponse {
@JsonProperty("picklist") List<User> userList;
}
@JsonProperty
是Jackson annotation的位置。如果您没有此注释,则可以随时将APIResponse更改为:
class APIResponse {
List<User> picklist;
}