Java Type Generic as Argument for GSON

前端 未结 13 1816
陌清茗
陌清茗 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:31

    In kotlin simple use for example:

    Get places function

    fun getPlaces (jsonString : String, clazz: Class) : T { val places : T = Gson().fromJson(jsonString,clazz) return places }

    Then you can use as:

    val places = getPlaces(Array::class.java)
    

提交回复
热议问题