我正在嘗試將此 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_.....
我不想忽略未知密鑰,因為我確實需要它。
uj5u.com熱心網友回復:
不應該@SerialName("")嗎?
https://kotlin.github.io/kotlinx.serialization/kotlinx-serialization-core/kotlinx-serialization-core/kotlinx.serialization/-serial-name/index.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/378281.html
