我无法将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"
}]
Run Code Online (Sandbox Code Playgroud)
而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
}
}
Run Code Online (Sandbox Code Playgroud)
并尝试以下代码会导致Exception:
com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_OBJECT,但位于第1行第2列路径$ BEGIN_ARRAY
代码:
var model = gson.fromJson<Array<GroupModel>>(inputString, GroupModel::class.java)
Run Code Online (Sandbox Code Playgroud)