BEGIN_ARRAY expected, but BEGIN_OBJECT on row 1 column 35

I just started with gson and I am trying to parse a JSON string that starts out as an object and always gets the same JSON error

{
  "code": 200, 
  "data": {
    "messages": [
      {
        "emailSender": "dsfd@mail.ru", 
        "countryCode": null, 
        "emailSenderReply": null, 
        "rejectedReason": null, 
        "messageReplySenderMessageDeleted": null, 
        "lastNameReceiver": null, 
        "wpMessagesRatingReplyId": null, 
        "wpMessagesRatingRating": null, 
        "countryMemberId": 143, 
        "phoneSenderReply": null, 
        "messageReplyReceiverMessageDeleted": null, 
        "readStatus": "unread", 
        "phoneReceiverReply": null, 
        "membersSenderUid": "m8692031", 
        "wpMessagesRequestTitle": "Fazzzzzz", 
        "title": "Fazzzzzz", 
        "countryTitle": null, 
        "emailReceiver": null, 
        "firstNameReceiverReply": null, 
        "id": 1288, 
        "messageReplyId": null, 
        "membersReceiverUid": "m1000002", 
        "time": "2014-12-28 14:32:09", 
        "wpMessagesRequestCategoryId": 4, 
        "lastNameReceiverReply": null, 
        "lastNameSender": "dsad", 
        "phoneReceiver": null, 
        "status": "unanswered", 
        "messageReplyReceiver": null, 
        "messageReplyStatus": null, 
        "memberReceiverRole": "admin", 
        "isConsultant": 1, 
        "roleReplyReceiver": null, 
        "wpMesssagesRequestCategoriesSystemName": "orders", 
        "lastNameSenderReply": null, 
        "memberSenderRole": "member", 
        "wpMesssagesRequestCategoriesName": "Orders", 
        "requestMessage": 1101, 
        "wpMessagesRequestPriority": "middle", 
        "messageReplyTime": null, 
        "message": "OLOLO", 
        "wpMessagesRequestCountryId": null, 
        "sender": 4481, 
        "firstNameReceiver": null, 
        "messageReplyMessage": null, 
        "firstNameSender": "asdas", 
        "firstNameSenderReply": null, 
        "emailReceiverReply": null, 
        "roleReplySender": null, 
        "messageReplySender": null, 
        "wpMessagesRequestProductId": null, 
        "receiver": 4364, 
        "isMessageForwarded": 0, 
        "wpMessagesRequestStatus": "not-taken", 
        "phoneSender": "2(342)-4-23-42", 
        "wpMessagesRequestMessage": "OLOLO"
      }
    ]
  }
}

      

and i keep running with error:

Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 3 column 12

      

The error in my log points to this line:

Gson gson = new GsonBuilder().create();
Main main=gson.fromJson(a, Main.class);

      

Here is my pojo

static class Main{
        int code;
        boolean error;
        List<Data> data;
    }
    static class Data{
        Enteties messages;
    }
    static class Enteties{
        String msg_title;
        String msg_time;
        int msg_id;
        String msg_status;
        @Override
        public String toString(){
            return msg_title+" "+msg_time+" "+msg_id+" "+msg_status;
        }
    }

      

+3


source to share


2 answers


Your main class has data as a list. Your JSON has this as an object. The types must match. If you are expecting only 1 data in the main, then don't use a list. If you are expecting 1 or more data, then do some code that generates the data sent across an array (even if there is only 1 object in that array).



+2


source


your POJOs should look something like this.

your Entity class:

public class Entity {
    private int id;

    private String emailSender;
    private String membersSenderUid;
    private String title;
    private String time;

    @SerializedName("wpMessagesRequestStatus")
    private String status;

    // getters, setters & toString methods
}

      

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

Your data class:



public class Data {
    private List<Entity> messages;

    // getters, setters & toString methods
}

      

Finally, the main class:

public class Main {
    private int code;
    private boolean error;
    private Data data;

    // getters, setters & toString methods
}

      

Here is 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);

      

0


source







All Articles