如何在Android中使用Gson解析内部JSON对象

时间:2017-04-24 12:43:18

标签: android json gson

我需要解析这个json,任何人都可以帮我做这个吗?

这里我能够使用gson无法解析此VehicleConfig *对象的数据对象???使用POJO类

2 个答案:

答案 0 :(得分:0)

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

此处vehicleConfigString是VehicleConfig节点的字符串版本,对象类型是POJO_CLASS_NAME.class变量。

答案 1 :(得分: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