ArtKorchagin
ArtKorchagin

Reputation: 4853

Retrofit - How parse array with different types of elements?

I receive this JSON string:

{
    "response": [
    346,
    {
        "id": 564,
        "from_id": -34454802,
        "to_id": -34454802,
        "date": 1337658196,
        "post_type": "post"
    },
    {
        "id": 2183,
        "from_id": -34454802,
        "to_id": -34454802,
        "date": 1423916628,
        "post_type": "post"
    },
    {
        "id": 2181,
        "from_id": -34454802,
        "to_id": -34454802,
        "date": 1423724270,
        "post_type": "post"
    }]
}

I create following classes:

public class Response {
    @SerializedName("response")
    ArrayList<Post> posts;
}

public class Post {
    int id;
    int from_id;
    int to_id;
    long date;
    String post_type;
}

When I try parse response, I get error:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was NUMBER at line 1 column 19 path $.response[0]

This because first element of array is Number. Which model is required to operate without error?

Upvotes: 0

Views: 1778

Answers (2)

learner
learner

Reputation: 3110

Your model class should be like this , Model objects always should be like string or ArrayList with class object or String Object.If you mention int ,you will get illegalState Exception.

public class Pojo
{
    private Response[] response;

    public Response[] getResponse ()
    {
        return response;
    }

    public void setResponse (Response[] response)
    {
        this.response = response;
    }
}


public class Response
{
    private String id;

    private String to_id;

    private String from_id;

    private String post_type;

    private String date;

    public String getId ()
    {
        return id;
    }

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

    public String getTo_id ()
    {
        return to_id;
    }

    public void setTo_id (String to_id)
    {
        this.to_id = to_id;
    }

    public String getFrom_id ()
    {
        return from_id;
    }

    public void setFrom_id (String from_id)
    {
        this.from_id = from_id;
    }

    public String getPost_type ()
    {
        return post_type;
    }

    public void setPost_type (String post_type)
    {
        this.post_type = post_type;
    }

    public String getDate ()
    {
        return date;
    }

    public void setDate (String date)
    {
        this.date = date;
    }
}

Upvotes: 1

Nikolay Nikiforchuk
Nikolay Nikiforchuk

Reputation: 2048

Retrofit don't work wit response directly it use Converter by default GsonConverter available. That is why custom Converter implementation required.

This post using-gson-to-parse-array-with-multiple-types should help with implementation.

To set converter just use:

RestAdapter getRestAdapter(...) {
    return new RestAdapter.Builder()
            ...
            .setConverter(converter)
            ...
            .build();
}

Upvotes: 1

Related Questions