0

for learning purpose I want to build a simple android REST client using retrofit and Gson. This is my Json string I want to parse:

{
  "kind": "Listing",
  "data": {
    "modhash": "qo2fwmjj5iee4adde638f97d5a7063087439bf4985aab4c9a5",
    "children": [
      {
        "kind": "t5",
        "data": {
          "banner_img": "",....

My data model:

public class Response {

    @SerializedName("kind")
    private String kind;

    @SerializedName("data")
    private Data data;

    public String getKind() {
        return kind;
    }

    public void setKind(String kind) {
        this.kind = kind;
    }


    public Data getData() {
        return data;
    }

    public void setData(Data data) {
        this.data = data;
    }
}

public class Data {

    @SerializedName("modhash")
    private String modhash;

    @SerializedName("after")
    private String after;

    @SerializedName("before")
    private String before;

    @SerializedName("children")
    private List<Child> children;

    public String getModhash() {
        return modhash;
    }

    public void setModhash(String modhash) {
        this.modhash = modhash;
    }

    public String getAfter() {
        return after;
    }

    public void setAfter(String after) {
        this.after = after;
    }

    public String getBefore() {
        return before;
    }

    public void setBefore(String before) {
        this.before = before;
    }

    public List<Child> getChildren() {
        return children;
    }

    public void setChildren(List<Child> children) {
        this.children = children;
    }
}

public class Child {
    @SerializedName("kind")
    private String kind;
    @SerializedName("data")
    private List<Sub> data;

    public String getKind() {
        return kind;
    }

    public void setKind(String kind) {
        this.kind = kind;
    }

    public List<Sub> getData() {
        return data;
    }

    public void setData(List<Sub> data) {
        this.data = data;
    }
}

Unit Test:

public class Test {

    private GsonConverter gsonConverter;
    private  Gson gson;

    private Logger logger = Logger.getLogger(Test.class.getName());

    public  Test() {
         gson = new GsonBuilder()
                .setDateFormat("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS'Z'")
                .create();
        gsonConverter = new GsonConverter(gson);
    }

    @Test
    public void testData() {
        Response response = gson.fromJson(TestUtil.RESPONSE_STRING, Response.class);
        logger.info("Data " + response.getData());

        Assert.assertNotNull(response.getData());
    }
}

So when the parser reaches the "children" attribute, following error occurs.

Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT
    at com.google.gson.internal.bind.JsonTreeReader.expect(JsonTreeReader.java:139)
    at com.google.gson.internal.bind.JsonTreeReader.beginArray(JsonTreeReader.java:58)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:79)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read

Don't know what to do, tried different things, do I have to handle the "children" by myself because of the [ bracket?

1 Answer 1

1

Field data in Child class shouldn't be a list. It is an object in json so make it object in data model.

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.