【问题标题】:Using @SerializedName with hyphen not working使用带有连字符的@SerializedName 不起作用
【发布时间】:2022-01-14 18:46:02
【问题描述】:

我正在尝试将此 JSON 响应反序列化为一个对象,并且我的一个键上有一个连字符。不幸的是,Kotlin 不支持变量名中的连字符,这就是我使用 @SerializedName() 的原因,但它现在仍然有效。 有什么线索说明原因吗?

JSON 响应

[
    {
        "dateCreated": "07-22-2021",
        "comments": "Comment",
        "vehicle_type": "Sedan",
        "name": "Leagacy Nissan Template",
        "template-type": "", //this is giving me the problem
        "template_uses_type": "Both"
        ...
    }
]

我的对象:

@Serializable
data class SpinDataResponse(
    val dateCreated:String,
    val comments: String,
    val vehicle_type:String,
    val name:String,
    @SerializedName("template-type") val template_type:String,
    val template_uses_type:String,
    ...
)

错误:

I/System.out:错误:偏移 120 处的意外 JSON 令牌:遇到 未知键“模板类型”。 在“Json {}”构建器中使用“ignoreUnknownKeys = true”来忽略未知键。 JSON 输入:....."name": "Nissan PathFinder", "template-type": "", "template_.....

我不想忽略未知密钥,因为我确实需要它。

【问题讨论】:

  • 你用什么反序列化 JSON?
  • 我正在使用 Kotlinx 序列化的 Ktor 客户端

标签: android kotlin ktor json-serialization ktor-client


【解决方案1】:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-11
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 1970-01-01
  • 1970-01-01
  • 2018-06-02
相关资源
最近更新 更多