I'm having some problems with a conversion of a Json object list to a Java List. I'm using the GSon to do it. I have the following Json list:
{"player":
[
{"id":"1","age":"25","name":"Neuer"},
{"id":"2","age":"26","name":"Cristiano Ronaldo"},
{"id":"3","age":"24","name":"Lionel Messi"}
]
}
And I have the following Java Object:
public class PlayerDTO implements Serializable {
private int id;
private String name;
private int age;
//Getters and Setters
}
In my Java class I'm doing:
Type collectionType = new TypeToken<ArrayList<PlayerDTO>>(){}.getType();
List<PlayerDTO> players = gson.fromJson(jsonString, collectionType);
But, when the Gson tries to convert the Json list I receive the error:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
at com.google.gson.Gson.fromJson(Gson.java:817)
at com.google.gson.Gson.fromJson(Gson.java:770)
at com.google.gson.Gson.fromJson(Gson.java:719)
at com.google.gson.Gson.fromJson(Gson.java:691)
at br.com.test.TesteGson.main(TesteGson.java:33)
Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:351)
at com.google.gson.internal.bind.ArrayTypeAdapter.read(ArrayTypeAdapter.java:70)
at com.google.gson.Gson.fromJson(Gson.java:805)
... 4 more
I understand that it happens because the Gson was expecting the Json without the "player" on the beginning of the Json String. Can you help me? Thanks!
PlayerCollection(?) type which contains an appropriate players list property..