定义 Data Class
1
2
3
4
5
6data class Topic(
@SerializedName("id") val id: Long,
@SerializedName("name") val name: String,
@SerializedName("image") val image: String,
@SerializedName("description") val description: String
)转换Json
1
2val gson = Gson()
val json = gson.toJson(topic)Json转Class
1
2val json = getJson()
val topic = gson.fromJson(json, Topic::class.java)