1
我有JSON和我想用GSON,我下面的JSON檢索:
{
"status_code":1,
"message":"response ok",
"api_version":"v1",
"api_endpoint_name":"category_island",
"data": [
{
"island_id":1,
"island_name":"Anambas",
"categories": [
{"id":1, "category_name":"Culinary"},
{"id":2, "category_name":"Hotel"},
{"id":3, "category_name":"Culture"}
]
}
]
}
之後,我創建的模型進行檢索:
public class ModelCategory {
@SerializedName("status_code")
public String status_code;
@SerializedName("message")
public String message;
@SerializedName("api_version")
public String api_version;
@SerializedName("api_endpoint_name")
public String api_endpoint_name;
@SerializedName("data")
public data data;
public static class data {
@SerializedName("id")
public String id;
@SerializedName("island_name")
public String island_name;
@SerializedName("categories")
public List<categories> categories;
public static class categories {
@SerializedName("ID")
public String id;
@SerializedName("category_name")
public String category_name;
}
}
}
,這是我的代碼來檢索JSON,我使用GSON:
ModelCategory model = new Gson().fromJson(models, ModelCategory.class);
ArrayList<ModelCategory.data.categories> _model = (ArrayList<ModelCategory.data.categories>) model.data.categories;
但是當我始終運行顯示錯誤我n ModelCategory,錯誤如下:
java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 6 column 11 path $.data
非常感謝,非常有幫助 – Bekti