Skip to content Skip to sidebar Skip to footer

Expected Begin_array But Was Begin_object At Line 1 Column 35

I freshly started with gson and i am trying to parse a JSON string which starts as an object and always get the same error JSON { 'code': 200, 'data': { 'messages': [

Solution 1:

Your Main class has Data as a list. Your JSON has it as an object. The types need to match. If you expect only 1 data in main, do not use a list. If you expect 1 or more datas, make the code that generates the data send down an array (even if that array only has 1 object in it).

Solution 2:

your POJOs should look something like this

your Entity class:

publicclassEntity{
    privateint id;

    privateString emailSender;
    privateString membersSenderUid;
    privateString title;
    privateString time;

    @SerializedName("wpMessagesRequestStatus")
    privateString status;

    // getters, setters & toString methods
}

@SerializedName is an annotation that indicates this member should be serialized to JSON with the provided name value as its field name. For more details check here

Your Data class:

publicclassData {
    privateList<Entity> messages;

    // getters, setters & toString methods
}

And finally Main class:

publicclassMain {
    privateint code;
    privateboolean error;
    private Data data;

    // getters, setters & toString methods
}

Here goes the parsing code:

Gson gson = new GsonBuilder().create();
Main main=gson.fromJson(jsonData, Main.class); // here jsonData is the string that is holding your actual json data
System.out.println(main);

Post a Comment for "Expected Begin_array But Was Begin_object At Line 1 Column 35"