I am converting JSON response of type array into java Object class, but while doing deserialization i am getting error as
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $ at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:200)
JSON Response
[
{
"name": "Apple iPhone X",
"price": 700,
"rating": 4,
"id": 1
},
{
"name": "Apple Mac Mini",
"price": 900,
"rating": 5,
"id": 2
},
{
"name": "HTC Chacha",
"price": 200,
"rating": 3,
"id": 4
},
{
"name": "Sony Xperia",
"price": 600,
"rating": 5,
"id": 5
},
{
"name": "Samsung Galaxy",
"price": 400,
"rating": 2,
"id": 6
},
{
"name": "LG LED 5600VW",
"price": 550,
"rating": 1,
"id": 7
},
{
"name": "Moto Razor",
"price": 65000,
"rating": 4,
"id": 9
}
]
Phones.Java (Object class model)
package apiEngine.model.responses;
public class Phones {
public String name;
public Integer price;
public Integer rating;
public Integer id;
public Phones() {
}
public Phones(String name, Integer price, Integer rating, Integer id) {
super();
this.name = name;
this.price = price;
this.rating = rating;
this.id = id;
}
}
Converting Method where JSON response is being converted into Java object
private static Phones phoneResponse;
public void displaylist() {
RequestSpecification request = RestAssured.given();
request.header("Content-Type", "application/json").header("x-access-token", token);
response = request.get("/products");
phoneResponse = response.getBody().as(Phones.class);
jsonString = response.asString();
//System.out.println("list of phone is displayed \n" + phoneResponse);
}