Using Gson, I am trying to de-serialize a nested, generic class. The structure of the class is as follows:
The Wrapper object is simplified, but usually contains other properties, such as statusMessage , that are returned with the data field from the server:
public class Response<T> { private List<T> data = null; public List<T> getData() { return this.data; } }
A simple class, the expected output from the data field is higher (although as an array):
public class Language { public String alias; public String label; }
Using:
Type type = new TypeToken<Response<Language>>() {}.getType(); Response<Language> response = new Gson().fromJson(json, type); List<Language> languages = response.getData(); Language l = languages.get(0); System.out.println(l.alias);
Where json -variable is something like this .
However, when doing this, I get the following exception (on line 3, the last code example):
ClassCastException: com.google.gson.internal.StringMap cannot be attributed to the book. Tongue
The exception ONLY occurs when saving data from getData() to a variable (or when used as one).
Any help would be greatly appreciated.
source share