Java Type Generic as Argument for GSON

前端 未结 13 1824
陌清茗
陌清茗 2020-11-27 12:51

In GSON to get a list of objects you do

Gson gson = new Gson();
Type token = new TypeToken>(){}.getType();
return gson.fromJson(json         


        
13条回答
  •  误落风尘
    2020-11-27 13:10

    This has been answered in previous questions. Basically, there are 2 options:

    1. Pass the Type in from the calling site. The calling code will use TypeToken or whatever to construct it.
    2. Construct a Type corresponding to the parameterized type yourself. This will require you to write a class that implements ParameterizedType

提交回复
热议问题