如何解析JSON以使用enum建模?

这是我的枚举类:

enum class VehicleEnumEntity(val value: String) {
   CAR("vehicle"),
   MOTORCYCLE("motorcycle"),
   VAN("van"),
   MOTORHOME("motorhome"),
   OTHER("other")
}

我需要把type解析成一个枚举

"vehicle": { "data": { "type": "vehicle", "id": "F9dubDYLYN" } }

EDIT

我try 了标准方法,只需将枚举传递给POJO,它总是空的

推荐答案

enum class VehicleEnumEntity(val value: String) {
   @SerializedName("vehicle")
   CAR("vehicle"),

   @SerializedName("motorcycle")
   MOTORCYCLE("motorcycle"),

   @SerializedName("van")
   VAN("van"),

   @SerializedName("motorhome")
   MOTORHOME("motorhome"),

   @SerializedName("other")
   OTHER("other")
}

Source

Kotlin相关问答推荐

如何在Kotlin中为两个数据类创建可重用的方法?

为什么在Spring中,对事务性方法调用的非事务方法调用仍然在事务中运行?

Spring Boot kotlin协程不能并行运行

用Quarkus和Reactor重写异步过滤器中的数据流

房间数据库操作中的协程取消

如何在数据类中删除空格 (String)?

在kotlin中匹配多个变量

判断 Kotlin 变量是否为函数

为什么 Kotlin 中的 Double 和 Long 类型不推荐直接转换为 Char?

如何在 Kotlin 中为变量分配另一个变量的值?

如何在 Kotlin 中实现 Composition UML 关系?

关于 Kotlin 函数类型转换的问题

这是什么 Kotlin 类型:(String..String?)

Kotlin 中多个 init 块的用例?

创建首选项屏幕时找不到androidx.preference.PreferenceScreen

是否可以在 kotlin 中嵌套数据类?

Kotlin 单元测试 - 如何模拟 Companion 对象的组件?

在 Kotlin 中取最后 n 个元素

无法解决:androidx.lifecycle:lifecycle-extensions-ktx:2.0.0-alpha1

Kotlin:获取文件的扩展名,例如.txt