2015-10-14 162 views
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 

回答

0

您需要更改模型。試試這個 -

Category.java

import com.google.gson.annotations.Expose; 
import com.google.gson.annotations.SerializedName; 

public class Category { 
    @SerializedName("id") 
    @Expose 
    private Integer id; 
    @SerializedName("category_name") 
    @Expose 
    private String categoryName; 
    public Integer getId() { 
     return id; 
    } 
    public void setId(Integer id) { 
     this.id = id; 
    } 
    public String getCategoryName() { 
     return categoryName; 
    } 
    public void setCategoryName(String categoryName) { 
     this.categoryName = categoryName; 
    } 
    @Override 
    public String toString() { 
     return "Category [id=" + id + ", categoryName=" + categoryName + "]"; 
    } 
} 

Datum.java

import java.util.ArrayList; 
import java.util.List; 
import com.google.gson.annotations.Expose; 
import com.google.gson.annotations.SerializedName; 

public class Datum { 
    @SerializedName("island_id") 
    @Expose 
    private Integer islandId; 
    @SerializedName("island_name") 
    @Expose 
    private String islandName; 
    @SerializedName("categories") 
    @Expose 
    private List<Category> categories = new ArrayList<Category>(); 
    public Integer getIslandId() { 
     return islandId; 
    } 
    public void setIslandId(Integer islandId) { 
     this.islandId = islandId; 
    } 
    public String getIslandName() { 
     return islandName; 
    } 
    public void setIslandName(String islandName) { 
     this.islandName = islandName; 
    } 
    public List<Category> getCategories() { 
     return categories; 
    } 
    public void setCategories(List<Category> categories) { 
     this.categories = categories; 
    } 
    @Override 
    public String toString() { 
     return "Datum [islandId=" + islandId + ", islandName=" + islandName 
       + ", categories=" + categories + "]"; 
    } 
} 

Example.java

import java.util.ArrayList; 
import java.util.List; 
import com.google.gson.annotations.Expose; 
import com.google.gson.annotations.SerializedName; 

public class Example { 
    @SerializedName("status_code") 
    @Expose 
    private Integer statusCode; 
    @SerializedName("message") 
    @Expose 
    private String message; 
    @SerializedName("api_version") 
    @Expose 
    private String apiVersion; 
    @SerializedName("api_endpoint_name") 
    @Expose 
    private String apiEndpointName; 
    @SerializedName("data") 
    @Expose 
    private List<Datum> data = new ArrayList<Datum>(); 
    public Integer getStatusCode() { 
     return statusCode; 
    } 
    public void setStatusCode(Integer statusCode) { 
     this.statusCode = statusCode; 
    } 
    public String getMessage() { 
     return message; 
    } 
    public void setMessage(String message) { 
     this.message = message; 
    } 
    public String getApiVersion() { 
     return apiVersion; 
    } 
    public void setApiVersion(String apiVersion) { 
     this.apiVersion = apiVersion; 
    } 
    public String getApiEndpointName() { 
     return apiEndpointName; 
    } 
    public void setApiEndpointName(String apiEndpointName) { 
     this.apiEndpointName = apiEndpointName; 
    } 
    public List<Datum> getData() { 
     return data; 
    } 
    public void setData(List<Datum> data) { 
     this.data = data; 
    } 
    @Override 
    public String toString() { 
     return "Example [statusCode=" + statusCode + ", message=" + message 
       + ", apiVersion=" + apiVersion + ", apiEndpointName=" 
       + apiEndpointName + ", data=" + data + "]"; 
    } 
} 

現在你可以測試它 -

Main.java

import com.google.gson.Gson; 
import com.google.gson.GsonBuilder; 
import com.testgson.beans.Example; 

public class Main { 
    private static Gson gson; 

    static { 
     gson = new GsonBuilder().create(); 
    } 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     String s = "{\"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\"}]}]}"; 
     Example info = gson.fromJson(s, Example.class); 
     System.out.println(info); 
    } 
} 

結果是 -

Example [statusCode=1, message=response ok, apiVersion=v1, apiEndpointName=category_island, data=[Datum [islandId=1, islandName=Anambas, categories=[Category [id=1, categoryName=Culinary], Category [id=2, categoryName=Hotel], Category [id=3, categoryName=Culture]]]]] 
+0

非常感謝,非常有幫助 – Bekti

相關問題