首页 > 解决方案 > 使用改造解析泛型类型列表

问题描述

我们使用改造、OkHttp 和 Gson 连接到我的 android 应用程序中的 API。

我们有一个带有以下响应的 API:

{
  inputs: [
     {
        type: "A",
        content:{
        ....
        },
        type: "B",
        content:{
        ....
        },
        type: "C",
        content:{
        ....
        }
     }
  ]
}

这些输入项是通用的,它们的类型是我在项目中拥有的静态类型之一。我已经有 5 种不同类型的内容(A、B、C、D、E)。

这是我上面 API 模型的数据类

data class inputs<T>(){
    @SerializedName("type")
    val type: String,
    @SeializedName("content")
    val content: T
}

如何将此 API 解析为这个通用输入数据类?

标签: parsinggenericsretrofit2

解决方案


推荐阅读