使用 Kotlin Koin 创建自定义数据 class 模型
Create customise Data class model using Kotlin Koin
我是 Kotlin 的新手,在我移动的过程中理解这些概念。卡在创建一种类型的数据 class 模型中,其中响应 json 结构如下所示
data class SPLPlayer(
@field:Json(name ="id") val playerId: String?,
val type: String?,
@field:Json(name ="value") val currentValue: String?,
@field:Json(name ="Confirm_XI") val isIn_XI: Boolean = false,
@field:Json(name ="Matches") val totalMatchs: String?,
@field:Json(name ="Position") val position: String?,
@field:Json(name ="Skill") val skill: String?,
@field:Json(name ="skill_name") val skillName: String?,
val teamId: String?,
val name: String?, // other keys to refer Name_Full, short_name
@field:Json(name ="Bowling") val bowler: SPLBowler? = null,
@field:Json(name ="Batting") val batsmen: SPLBatsmen? = null
)
data class SPLTeamInfo (
**How to parse the Team object which is dictionary**
)
感谢并感谢每一位 reader。期待解决方案。
您应该能够通过向 setter @set:JsonDeserialize()
添加注释并传递您自己的反序列化器实现来使用您自己的反序列化器。
沿着:
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import com.fasterxml.jackson.databind.JsonDeserializer
.. rest of imports
// for a given simplified json string
val json: String = """{"teams":{"1":{"name":"foo"},"2":{"name":"bar"}}}"""
class MyDeserializer : JsonDeserializer<List<Team>> {
override fun deserialize(json: JsonElement?, typeOfT: Type?, context: JsonDeserializationContext?): List<Team>? {
// iterate over each json element and return list of parsed teams
}
}
data class JsonResp (
@set:JsonDeserialize(using = MyDeserializer::class)
var teams: List<Team>
)
data class Team (
var id: String, // this is going to be a team key
var name: String
)
已尝试 GitHub 使用查询 @set:JsonDeserialize
进行搜索,它显示了数千个示例。
我是 Kotlin 的新手,在我移动的过程中理解这些概念。卡在创建一种类型的数据 class 模型中,其中响应 json 结构如下所示
data class SPLPlayer(
@field:Json(name ="id") val playerId: String?,
val type: String?,
@field:Json(name ="value") val currentValue: String?,
@field:Json(name ="Confirm_XI") val isIn_XI: Boolean = false,
@field:Json(name ="Matches") val totalMatchs: String?,
@field:Json(name ="Position") val position: String?,
@field:Json(name ="Skill") val skill: String?,
@field:Json(name ="skill_name") val skillName: String?,
val teamId: String?,
val name: String?, // other keys to refer Name_Full, short_name
@field:Json(name ="Bowling") val bowler: SPLBowler? = null,
@field:Json(name ="Batting") val batsmen: SPLBatsmen? = null
)
data class SPLTeamInfo (
**How to parse the Team object which is dictionary**
)
感谢并感谢每一位 reader。期待解决方案。
您应该能够通过向 setter @set:JsonDeserialize()
添加注释并传递您自己的反序列化器实现来使用您自己的反序列化器。
沿着:
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import com.fasterxml.jackson.databind.JsonDeserializer
.. rest of imports
// for a given simplified json string
val json: String = """{"teams":{"1":{"name":"foo"},"2":{"name":"bar"}}}"""
class MyDeserializer : JsonDeserializer<List<Team>> {
override fun deserialize(json: JsonElement?, typeOfT: Type?, context: JsonDeserializationContext?): List<Team>? {
// iterate over each json element and return list of parsed teams
}
}
data class JsonResp (
@set:JsonDeserialize(using = MyDeserializer::class)
var teams: List<Team>
)
data class Team (
var id: String, // this is going to be a team key
var name: String
)
已尝试 GitHub 使用查询 @set:JsonDeserialize
进行搜索,它显示了数千个示例。