The Camster
The Camster

Reputation: 1929

How do I parse a root JSON Array with restassured?

I see that I can do the following with restassured. Given JSON:

{"locationId"=456,"name"="Home"}

I can get an object representing that json like this:

Location location = given().headers(headers).when().expect().statusCode(200).get(getUrl(urlQualifier)).as(Location.class);

How do I parse this JSON is I receive a root array of my Location objects in JSON. So, given this JSON:

[{"locationId"=1,name="Home"},{"locationId"=2,name="Work"}]

I want to parse out a List object. The following of course is a compile error, but it demonstrates what I am trying to do:

List<Location> list = given().headers(headers).when().expect().statusCode(200).get(getUrl(urlQualifier)).as((List<Location>).class);

Upvotes: 1

Views: 1671

Answers (1)

spg
spg

Reputation: 9847

Try deserializing it to a Java array:

Location[] list = given().headers(headers).when().expect().statusCode(200).get(getUrl(urlQualifier)).as(Location[].class);

Upvotes: 3

Related Questions