Gson在Kotlin中反序列化整数数组 [英] Gson deserialize Array of Integers in kotlin
本文介绍了Gson在Kotlin中反序列化整数数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
通常,当我反序列化json-String时,我会使用如下代码:
Normally, when I deserialize a json-String I use something like this:
val result = gson.fromJson<myObject>(json, object : TypeToken<myObject>() {}.type)
但是现在我想反序列化一个简单的Int列表,并且我不能为此创建对象.
But now I want to deserialize a simple List of Int and I cannot build an object for that.
json-String非常简单,看起来像这样:
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)
不起作用,因为没有对象.我应该如何处理这种简单的结构?没有gson会更好,例如带有爆炸?
does not work because there is no object. How should I handle such an easy structure? Is it better without gson eg with explode?
推荐答案
我认为您在Kotlin中不需要使用TypeToken就可以使用
I think you in kotlin you don't need to use TypeToken you can go like
val result = gson.fromJson<List<Int>>(json)
如果您想阅读更多
这篇关于Gson在Kotlin中反序列化整数数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文