kotlinx-serialization-json Please ensure that class is marked as '@Serializable' and that the serialization compiler plugin is applied.
Please ensure that class is marked as '@Serializable' and that the serialization compiler plugin is applied.
@Serializable data class PersonModel(val name:String,val age:Int) val personModel = PersonModel("xxx", 18) val json = Json.encodeToString(personModel) println("json:$json")
需要默认值的(或者 字段加 @EncodeDefault)
Json{encodeDefaults=true}.encodeToString(personModel)
主目录
plugins {
...
kotlin("plugin.serialization") version "1.5.31" apply false
}
模块
plugins {
....
kotlin("plugin.serialization")
}
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.9.0")
参考:https://dev59.com/yVR1hooBIXSAPYfQdMeZ
参考:https://www.baeldung-cn.com/kotlin/data-class-json-serialize-default-values
浙公网安备 33010602011771号