0

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.

2
  • I think problem is your json object. Delete [ { "day": 16, "orders": 1 } ] then it works fine Commented Mar 6, 2020 at 12:44
  • You need to Code your own custom TypeAdaptor for GSON you have created. Following link may help you: tutorialspoint.com/gson/gson_custom_adapters.htm Commented Mar 6, 2020 at 19:05

1 Answer 1

1

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 .

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.