Kotlin convert json array to model list using GSON

后端 未结 3 717
一个人的身影
一个人的身影 2021-02-13 05:15

I am not able to convert a JSON Array into a GroupModel array. Below is the JSON I used:

[{
  \"description\":\"My expense to others\",
  \"items\"         


        
3条回答
  •  隐瞒了意图╮
    2021-02-13 05:37

    You need to use a TypeToken to capture the generic type of the array, and you need this to be the type that GSON sees as the target, instead of just GroupModel::class it is really a list of those. You can create a TypeToken and use it as follows:

    Type groupListType = new TypeToken>() {}.getType();
    var model = gson.fromJson(inputString, groupListType);
    

提交回复
热议问题