You can do this:
List<Artwork> returnedArtworks = Arrays.asList(response.getBody().as(Artwork[].class));
The trick is to deserialize JSON to an array of objects (because there is no difference between the JSON string of an array or a list), then convert the array to a list.
Answer from volatilevar on Stack OverflowYou can do this:
List<Artwork> returnedArtworks = Arrays.asList(response.getBody().as(Artwork[].class));
The trick is to deserialize JSON to an array of objects (because there is no difference between the JSON string of an array or a list), then convert the array to a list.
this solution works for version 3.0.2 (io.restassured):
JsonPath jsonPath = RestAssured.given()
.when()
.get("/order")
.then()
.assertThat()
.statusCode(Response.Status.OK.getStatusCode())
.assertThat()
.extract().body().jsonPath();
List<Order> orders = jsonPath.getList("", Order.class);
This will extract the objects for a structure like this:
public class Order {
private String id;
public String getId(){
return id; }
public void setId(String id){
this.id = id;
}
}
with the given json:
[
{ "id" : "5" },
{ "id" : "6" }
]
Videos
I found a way to achieve what I wanted:
List<Person> persons = given().when().get("person/").as(Person[].class);
UPDATE: Using Rest-Assured 1.8.1, looks like cast to List is not supported anymore. You need to declare and object array like this:
Person[] persons = given().when().get("person/").as(Person[].class);
To extract a Java List, and not an Array, from a JSON API response, you just have to remember to use jsonPath rather than as:
List<Person> persons = given()
.when()
.get("/person")
.then()
.extract()
.body()
// here's the magic
.jsonPath().getList(".", Person.class);
Your json path can point to anywhere you expect to have a list of json objects in your body. in this example (and working for your question) it just points to the json root.
sidenode: rest-assured is internally using jackson for deserialization (for .jsonPath as well as .as)