我有这样的Java POJO类:
class Topic { @SerializedName("id") long id; @SerializedName("name") String name; }
我有一个像这样的Kotlin数据类
data class Topic(val id: Long, val name: String)
如何在java变量中提供json key给kotlin data class诸如@SerializedName注解之类的任何变量?
json key
kotlin data class
@SerializedName
资料类别:
data class Topic( @SerializedName("id") val id: Long, @SerializedName("name") val name: String, @SerializedName("image") val image: String, @SerializedName("description") val description: String )
到JSON:
val gson = Gson() val json = gson.toJson(topic)
从JSON:
val json = getJson() val topic = gson.fromJson(json, Topic::class.java)