预期为 BEGIN_ARRAY 但在第 1 行第 35 列为 BEGIN_OBJECT

Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 35

我刚开始使用 gson,我正在尝试解析一个 JSON 字符串,该字符串以对象开头,但总是出现相同的错误 JSON

{
  "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"
      }
    ]
  }
}

我一直运行进入错误:

Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 3 column 12

我的日志中的错误指向这一行:

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

这是我的 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;
        }
    }

您的 Main class 有数据列表。您的 JSON 将其作为对象。类型需要匹配。如果您希望在 main 中只有 1 个数据,请不要使用列表。如果您期望 1 个或多个数据,请使生成数据的代码向下发送一个数组(即使该数组中只有 1 个对象)。

您的 POJO 应如下所示

您的实体 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 是一个注解,指示该成员应序列化为 JSON,并使用提供的名称值作为其字段名称。更多详情 check here

您的数据class:

public class Data {
    private List<Entity> messages;

    // getters, setters & toString methods
}

最后是 Main class:

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

    // getters, setters & toString methods
}

解析代码如下:

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