2016-02-19 60 views
0

我試圖得到一些項目出來的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

郵遞員中添加用戶實體 –

+0

檢查與網址,而params。網址或服務器響應必定存在問題。 –

回答

2

你應該使用這種情況下的自定義響應:

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

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

@JsonPropertyJackson annotation。如果你沒有這個註解你可以隨時將APIResponse更改爲:

class APIResponse { 
    List<User> picklist; 
} 
+0

^解決了它。謝謝:) – Haruspik

+0

歡迎您,我很高興它幫助:) – FlyingPumba