【问题标题】:Moshi parse json with different keyMoshi 用不同的键解析 json
【发布时间】:2020-05-13 08:39:49
【问题描述】:

我一直在寻找PolymorphicAdapter,但我能找到的所有多态示例都有一个名为“type”的键或类似的键,可用于区分要使用的类。但是就我而言,我没有这样的钥匙。我对如何解析如此奇特的 json 有点迷茫。

{
    "infos": {
        "1588318": {
            "id": "1588318",
            "id_user": "9701",
            "profile_name": "Profile1",
            "views": 100
        },
        "1588319": {
            "id": "1588319",
            "id_user": "7391",
            "profile_name": "Profile2",
            "views": 10
        },
        "1588320": false,
        "1588321": {
            "id": "1588321",
            "deleted": true
        }
    }
}
data class UserInfo(val infos: Map<String, UserResult>)

sealed class UserResult {

    data class UserDeleted(val id: String, val deleted: Boolean): UserResult()
    data class UserInfoCard(
        val id: String,
        val title: String,
        @Json(name = "profile_name") val profileName: String,
        val views: Int
    ): UserResult()
    
}

【问题讨论】:

    标签: json kotlin moshi


    【解决方案1】:

    最后我没有找到任何解决方案,在与 API 经理讨论后,他说他会更新一个密钥以确定它是 profile 还是 deleted_profile

    【讨论】:

      猜你喜欢
      • 2016-08-31
      • 1970-01-01
      • 1970-01-01
      • 2018-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-10
      • 2021-04-24
      相关资源
      最近更新 更多