改造 - 如何忽略 table 名称

Retrofit - how to ignore table name

我正在尝试从 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();
}

但是因为 table 有一个名字,它会抛出一个异常(至少我认为这是原因):

java.lang.IllegalStateException:
Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 3 path $

我该如何解决?

对于这种情况,您应该使用自定义响应:

public interface MyAPI {
    @GET("/?country=true")
    Call<APIResponse> getUsers();
}

class APIResponse {
    @JsonProperty("picklist") List<User> userList;
}

其中 @JsonPropertyJackson annotation。如果您没有此注释,您可以随时将 APIResponse 更改为:

class APIResponse {
    List<User> picklist;
}