Simon-Pierre Simon-Pierre - 10 months ago 150
JSON Question

REST Assured - Generic List deserialization

Let's say I have a Java


class Person {
String name;
String email;

With REST Assured, you can deserialize this JSON object

{"name":"Bob", "email":""}

to a Java
instance using

Person bob = given().when().get("person/Bob/").as(Person.class);

How does one use REST Assured to deserialize this JSON array

[{"name":"Bob", "email":""},
{"name":"Alice", "email":""},
{"name":"Jay", "email":""}]

into a
? For example, this would be handy:

List<Person> persons = given().when().get("person/").as(...);

Answer Source

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