Java 来自 Json 的 Kotlin 数据类使用 GSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44117970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Kotlin Data Class from Json using GSON
提问by erluxman
I have Java POJO class like this:
我有这样的 Java POJO 类:
class Topic {
@SerializedName("id")
long id;
@SerializedName("name")
String name;
}
and I have a Kotlin data class Like this
我有一个 Kotlin 数据类像这样
data class Topic(val id: Long, val name: String)
How to provide the json key
to any variables of the kotlin data class
like the @SerializedName
annotation in java variables ?
如何向java变量中的注解json key
之kotlin data class
类的任何变量提供@SerializedName
?
采纳答案by Anton Holovin
Data class:
数据类:
data class Topic(
@SerializedName("id") val id: Long,
@SerializedName("name") val name: String,
@SerializedName("image") val image: String,
@SerializedName("description") val description: String
)
to JSON:
到 JSON:
val gson = Gson()
val json = gson.toJson(topic)
from JSON:
来自 JSON:
val json = getJson()
val topic = gson.fromJson(json, Topic::class.java)
回答by Vasily Bodnarchuk
Based on answer of Anton Golovin
基于Anton Golovin 的回答
Details
细节
- Gson version: 2.8.5
- Android Studio 3.1.4
- Kotlin version: 1.2.60
- Gson 版本:2.8.5
- 安卓工作室 3.1.4
- 科特林版本:1.2.60
Solution
解决方案
Create any class data and inherit JSONConvertableinterface
创建任意类数据并继承JSONConvertable接口
interface JSONConvertable {
fun toJSON(): String = Gson().toJson(this)
}
inline fun <reified T: JSONConvertable> String.toObject(): T = Gson().fromJson(this, T::class.java)
Usage
用法
Data class
数据类
data class User(
@SerializedName("id") val id: Int,
@SerializedName("email") val email: String,
@SerializedName("authentication_token") val authenticationToken: String) : JSONConvertable
From JSON
来自 JSON
val json = "..."
val object = json.toObject<User>()
To JSON
到 JSON
val json = object.toJSON()
回答by Pawan Soni
You can use similar in Kotlin class
您可以在 Kotlin 类中使用类似的
class InventoryMoveRequest {
@SerializedName("userEntryStartDate")
@Expose
var userEntryStartDate: String? = null
@SerializedName("userEntryEndDate")
@Expose
var userEntryEndDate: String? = null
@SerializedName("location")
@Expose
var location: Location? = null
@SerializedName("containers")
@Expose
var containers: Containers? = null
}
And also for nested class you can use same like if there is nested object. Just provide Serialize name for the Class.
而且对于嵌套类,您可以使用与嵌套对象相同的方法。只需为类提供序列化名称。
@Entity(tableName = "location")
class Location {
@SerializedName("rows")
var rows: List<Row>? = null
@SerializedName("totalRows")
var totalRows: Long? = null
}
so if get response from the server each key will map with JOSN.
所以如果从服务器得到响应,每个键都将与 JOSN 映射。
Alos, convert List to JSON:
Alos,将列表转换为 JSON:
val gson = Gson()
val json = gson.toJson(topic)
ndroid convert from JSON to Object:
ndroid 从 JSON 转换为 Object:
val json = getJson()
val topic = gson.fromJson(json, Topic::class.java)