This question already has answers here:
Enum annotations in Kotlin

(2个答案)


3年前关闭。




如何解析JSON以进行枚举建模?

这是我的枚举类:
enum class VehicleEnumEntity(val value: String) {
   CAR("vehicle"),
   MOTORCYCLE("motorcycle"),
   VAN("van"),
   MOTORHOME("motorhome"),
   OTHER("other")
}

我需要将type解析为一个枚举



编辑

我尝试了标准方式,只需将我的枚举传递给POJO,它始终为null

最佳答案

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

08-05 18:59