出现异常,无法从数组值反序列化“response.BookingIDList”类型的值(令牌“JsonToken.START_ARRAY”)

Sus*_*han 5 rest selenium json

I am trying to deserialize the response using POJO classes.
My sample response is;
Run Code Online (Sandbox Code Playgroud)

[ { "bookingid": 4 }, { "bookingid": 9 }, { "bookingid": 3 }, { "bookingid": 2 }, { "bookingid": 1 }, { "bookingid": 6 }, { “bookingid”:5},{“bookingid”:10},{“bookingid”:8},{“bookingid”:7}]

    public class BookingID
    {
        private int bookingid;
    
        public void setId(int bookingid) {
            this.bookingid = bookingid;
        }
    
        public int getId() {
            return bookingid;
        }
    
        @Override
        public String toString() {
            return "BookingID{" +
                    "bookingid=" + bookingid +
                    '}';
        }

}
Run Code Online (Sandbox Code Playgroud)

预订列表类

import java.util.List;

public class BookingIDList
{
    private List<BookingID> bookingList;

    public void setBookingList(List<BookingID> bookingList) {
        this.bookingList = bookingList;
    }

    public List<BookingID> getBookingList() {
        return bookingList;
    }

    @Override
    public String toString() {
        return "BookingIDList{" +
                "bookingList=" + bookingList +
                '}';
    }
}


**I am now trying to deserialize** 
Run Code Online (Sandbox Code Playgroud)

响应response=给定(requestSpecificationGenerator.GetRequestSpecification()).get(“/预订”); System.out.println("所有预订如下\n"+response.asPrettyString());

    BookingIDList bookingIDList = response.getBody().as(BookingIDList.class);
**getting the exception here as**
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `response.BookingIDList` from Array value (token `JsonToken.START_ARRAY`)
 at [Source: (String)"[
    {
        "bookingid": 4
    },
    {
        "bookingid": 9
    },
    {
        "bookingid": 3
    },
    {
        "bookingid": 2
    },
    {
        "bookingid": 1
    },
    {
        "bookingid": 6
    },
    {
        "bookingid": 5
    },
    {
        "bookingid": 10
    },
    {
        "bookingid": 8
    },
    {
        "bookingid": 7
    }
]"; line: 1, column: 1]
    at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59)
Run Code Online (Sandbox Code Playgroud)

lrv*_*lrv 5

警告指出它需要一个对象,而不是集合/数组,根据 BookingIDList POJO 定义,它会类似于:

{"bookingList": [ { "bookingid": 4 }, { "bookingid": 9 }, { "bookingid": 3 }, { "bookingid": 2 }, { "bookingid": 1 }, { "bookingid" : 6 }, { "bookingid": 5 }, { "bookingid": 10 }, { "bookingid": 8 }, { "bookingid": 7 } ] }

我猜你不希望这样,所以你可能想使用 as.(TypeRef ) 而不是 as(Class cls)

 /**
     * Get the body as a container with a generic type. Note that this may not work for JAXB (XML) or HTML.
     *
     * @return The object
     */
    <T> T as(TypeRef<T> typeRef);
Run Code Online (Sandbox Code Playgroud)
List<BookingID> bookingIds = response.getBody().as(new TypeRef<List<BookingID>>() {
        });
Run Code Online (Sandbox Code Playgroud)