从数据库中检索列表时,我得到“预期的BEGIN_ARRAY但在第1行第1列的路径$处为STRING”。如何解决此错误,请提前在代码中输入错误。

我得到的Json回应如下,

   [{
    "eventId":1,
    "ringeeUserId":2,
    "text":"Reception1",
    "place":"Erode",
    "eventDate":"2015-10-03",
    "startTime":"09:00 AM",
    "endTime":"12:00 PM",
    "isActive":0,
    "isDelete":0,
    "eventUserRelationBOs":[]
    }]


下面是EventBO类文件,

 package com.ringeeapp.service.businessobjects;

    import java.io.Serializable;
    import java.util.List;

    public class EventBO implements Serializable {

    private static final long serialVersionUID = 281625146097131515L;

    private long eventId;

    private long ringeeUserId;

    private String text;

    private String place;

    private String eventDate;

    private String startTime;

    private String endTime;

    private int isActive;

    private int isDelete;

    private List<EventUserRelationBO> eventUserRelationBOs;

    public long getEventId() {
        return eventId;
    }

    public void setEventId(long eventId) {
        this.eventId = eventId;
    }

    public long getRingeeUserId() {
        return ringeeUserId;
    }

    public void setRingeeUserId(long ringeeUserId) {
        this.ringeeUserId = ringeeUserId;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getPlace() {
        return place;
    }

    public void setPlace(String place) {
        this.place = place;
    }

    public String getEventDate() {
        return eventDate;
    }

    public void setEventDate(String eventDate) {
        this.eventDate = eventDate;
    }

    public String getStartTime() {
        return startTime;
    }

    public void setStartTime(String startTime) {
        this.startTime = startTime;
    }

    public String getEndTime() {
        return endTime;
    }

    public void setEndTime(String endTime) {
        this.endTime = endTime;
    }

    public int getIsDelete() {
        return isDelete;
    }

    public void setIsDelete(int isDelete) {
        this.isDelete = isDelete;
    }

    public int getIsActive() {
        return isActive;
    }

    public void setIsActive(int isActive) {
        this.isActive = isActive;
    }

    public List<EventUserRelationBO> getEventUserRelationBOs() {
        return eventUserRelationBOs;
    }

    public void setEventUserRelationBOs(List<EventUserRelationBO> eventUserRelationBOs) {
        this.eventUserRelationBOs = eventUserRelationBOs;
    }

}


下面的代码处理json数据

public @ResponseBody String getAllInvites(@RequestParam("userBO") String userBo) {
        List<EventBO> eventBOs = new ArrayList<EventBO>();
        UserBO userBO = gson.fromJson(userBo, UserBO.class);
        try {
            eventBOs = manageEventServiceImpl.getAllInvites(userBO);
            log.info("getting all events for user " + userBO.getRingeeUserId());
        } catch (UserServiceException serExp) {
            log.error("Error while getting event for userId id" + userBO.getRingeeUserId(), serExp);
        }
        return gson.toJson(eventBOs);
    }

最佳答案

您有一个EventBO数组,但尝试将其作为对象读取。如果您像这样解组:

EventBO bo = gson.fromJson(JSON, EventBO.class);


比您将收到错误“ java.lang.IllegalStateException:预期为BEGIN_OBJECT,但在行1列2路径$处为BEGIN_ARRAY”

但是,如果将代码更改为:

EventBO[] list = gson.fromJson(JSON, EventBO[].class);


您成功解组了这个json

附言经过Gson 2.4测试

关于java - 如何解决“预期BEGIN_ARRAY但在第1行第1列路径$处为STRING”的问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33449280/

10-09 08:21