Gson deserialize Array of Integers in kotlin

馋奶兔 提交于 2020-08-10 19:42:05

问题


Normally, when I deserialize a json-String I use something like this:

val result = gson.fromJson<myObject>(json, object : TypeToken<myObject>() {}.type)

But now I want to deserialize a simple List of Int and I cannot build an object for that.

The json-String is extremely simple and looks like this:

[1,35,37,255]

and I would like to save it into a List but

val result = gson.fromJson<List<Int>>(json, object : TypeToken<List<Int>>() {}.type)

does not work because there is no object. How should I handle such an easy structure? Is it better without gson eg with explode?


回答1:


I think you in kotlin you don't need to use TypeToken you can go like

     val result = gson.fromJson<List<Int>>(json)

here if you want to read more



来源:https://stackoverflow.com/questions/57972483/gson-deserialize-array-of-integers-in-kotlin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!