2017-04-24 73 views
-1

我需要解析這個JSON,任何人都可以幫助我做到這一點?如何在Android中使用Gson解析內部JSON對象

這裏我能夠使用gson的數據對象無法解析此VehicleConfig *對象?使用POJO類

+0

輕鬆地讀取它爲你的內部JSON對象單獨的POJO。 –

+0

...並使用組合將內部pojo對象放置到外部一個 – devnull69

+0

由於您反序列化到的POJO不起作用,顯示?a – nbokmans

回答

0

請按照下列步驟操作

步驟1.

創建一個新變量一樣,

public class Data { 

     // your rest variables 

     @SerializedName("VehicleConfig") 
     @Expose 
     private VehicleConfig vehicleConfig; 

     // getter & setter method 

     public void setVehicleImage(Integer vehicleImage) { 
      this.vehicleImage = vehicleImage; 
     } 

     public VehicleConfig getVehicleConfig() { 
       return vehicleConfig; 
     } 

} 

步驟2.

與 「VehicleConfig」 的名稱創建一個新類。

public class VehicleConfig { 

     @SerializedName("Id") 
     @Expose 
     private Integer id; 
     @SerializedName("Title") 
     @Expose 
     private String title; 
     @SerializedName("RegularSeats") 
     @Expose 
     private Integer regularSeats; 
     @SerializedName("RegularWheelChairs") 
     @Expose 
     private Integer regularWheelChairs; 
     @SerializedName("BariatricsWheelChair") 
     @Expose 
     private Integer bariatricsWheelChair; 

     public Integer getId() { 
      return id; 
     } 

     public void setId(Integer id) { 
      this.id = id; 
     } 

     public String getTitle() { 
      return title; 
     } 

     public void setTitle(String title) { 
      this.title = title; 
     } 

     public Integer getRegularSeats() { 
      return regularSeats; 
     } 

     public void setRegularSeats(Integer regularSeats) { 
      this.regularSeats = regularSeats; 
     } 

     public Integer getRegularWheelChairs() { 
      return regularWheelChairs; 
     } 

     public void setRegularWheelChairs(Integer regularWheelChairs) { 
      this.regularWheelChairs = regularWheelChairs; 
     } 

     public Integer getBariatricsWheelChair() { 
      return bariatricsWheelChair; 
     } 

     public void setBariatricsWheelChair(Integer bariatricsWheelChair) { 
      this.bariatricsWheelChair = bariatricsWheelChair; 
     } 

     @Override 
     public String toString() { 
      return ToStringBuilder.reflectionToString(this); 
     } 
    } 

步驟3.

現在你可以像

Data data = // your values 

VehicleConfig vehicleConfig = data.getVehicleConfig(); 

String title = vehicleConfig.getTitle(); 
Integer id = vehicleConfig.getId(); 
// you can read rest values 
+0

刪除您的代碼段底部的版權代碼 –

+0

刪除,現在看到我更新的答案。 –

+0

我可否知道downvote的任何理由? –

0
Gson gson = new Gson(); 
    JsonElement jsonElement = gson.toJsonTree(vehicleConfigString); 
    return gson.fromJson(jsonElement,objectType); 

這裏vehicleConfigString是VehicleConfig節點和對象類型的字符串版本是POJO_CLASS_NAME.class變量。