简体   繁体   中英

build model for parsing Json containing nested array using GSON

i used json to pojo tools in order to parse the following json :

{
  "result": [
    {
      "orders": 5
    },
    {
      "orders": 20
    },
    [
      {
        "day": 16,
        "orders": 1
      }
    ]
  ]
}

json to pojo tools has generated the following two models

public class Result {

  @SerializedName("orders")
  @Expose
  private Long orders;

  public Long getOrders() {
      return orders;
  }

  public void setOrders(Long orders) {
      this.orders = orders;
  }
}

public class Example {

  @SerializedName("result")
  @Expose
  private List<Result> result = null;

  public List<Result> getResult() {
      return result;
  }

  public void setResult(List<Result> result) {
      this.result = result;
  }
}

However, it fails to parse the last json format, specially the nested array as it didn't include it in generated models. any body can assist ? Your response is appreciated.

try this after edit your Example class

public class Example {

    @SerializedName("result")
    @Expose
    private List<Result> result = null;

    public List<Result> getResult() {
        return result;
    }

    public void setResult(List<Result> result) {
        this.result = result;
    }

    public class Result {

        @SerializedName("orders")
        @Expose
        private Integer orders;

        public Integer getOrders() {
            return orders;
        }

        public void setOrders(Integer orders) {
            this.orders = orders;
        }

    }
}

I hope it will work with you .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM