问题
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