【发布时间】:2018-02-18 16:11:11
【问题描述】:
我无法将 JSON 数组转换为 GroupModel 数组。下面是我使用的 JSON:
[{
"description":"My expense to others",
"items":["aaa","bbb"],
"name":"My Expense"
},
{
"description":"My expense to others","
items":["aaa","bbb"],
"name":"My Expense"
}]
GroupModel 类是:
class GroupModel {
var name: String? = null
var description: String? = null
var items: MutableList<String>? = null
constructor(name: String, description: String, items: MutableList<String>) {
this.name = name
this.description = description
this.items = items
}
}
并尝试以下代码会导致Exception:
com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期 BEGIN_OBJECT 但在第 1 行第 2 列路径 $
处为 BEGIN_ARRAY
代码:
var model = gson.fromJson<Array<GroupModel>>(inputString, GroupModel::class.java)
【问题讨论】: