使用GSON的Json的Kotlin数据类
我有这样的Java POJO类:
class Topic { @SerializedName("id") long id; @SerializedName("name") String name; }
我有一个Kotlin数据类像这样
data class Topic(val id: Long, val name: String)
如何提供json selector
的任何variables的kotlin data class
像@SerializedName
注释在Javavariables?
数据类:
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)