Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
684 views
in Technique[技术] by (71.8m points)

android - 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": [
      {
        "emailSender": "[email protected]", 
        "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 into the 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;
        }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...