I want to transfer a list object via Google Gson, but I don\'t know how to deserialize generic types.
What I tried after looking at this (BalusC\'s answer):
Another way is to use an array as a type, e.g.:
MyClass[] mcArray = gson.fromJson(jsonString, MyClass[].class);
This way you avoid all the hassle with the Type object, and if you really need a list you can always convert the array to a list by:
List<MyClass> mcList = Arrays.asList(mcArray);
IMHO this is much more readable.
And to make it be an actual list (that can be modified, see limitations of Arrays.asList()
) then just do the following:
List<MyClass> mcList = new ArrayList<>(Arrays.asList(mcArray));