您可以在 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
}
而且对于嵌套类,如果有嵌套对象,您也可以使用相同的方法。只需为类提供序列化名称。
@Entity(tableName = "location")
class Location {
@SerializedName("rows")
var rows: List<Row>? = null
@SerializedName("totalRows")
var totalRows: Long? = null
}
因此,如果从服务器获得响应,每个键都将映射到 JOSN。
另外,将 List 转换为 JSON:
val gson = Gson()
val json = gson.toJson(topic)
android 从 JSON 转换为 Object:
val json = getJson()
val topic = gson.fromJson(json, Topic::class.java)