【发布时间】:2019-05-20 07:20:52
【问题描述】:
我正在更新对使用序列化数据类解析 JSON 响应的 API 响应的解析。序列化现在工作得很好,但是我试图解析成数据类的新数据并不完全依赖于 json 中的数据。这就是我的意思:
数据类是Career,我需要解析的新数据是一组skills,每个都有一个rating。 json数据很简单,包含的技能如下:
{
// other career data
...
"mathematics_skill": 8,
"critical_thinking_skill": 6
... // the remaining skills
}
使用直接序列化,我只能这样存储数据:
data class Career(
// Other career data
@serializableName("mathematic_skill") val mathSkill: Int,
@serializableName("critical_thinking_skill") val mathSkill: Int,
// remaining skills
)
但是,我想将所有技能存储在自定义技能类的数组变量中,该变量不仅包含评级,还包含技能名称和颜色。基本上,当我访问职业的技能数据时,我想像这样访问它:
val careerMathSkill = career.skills[0]
val mathRating = careerMathSkill.rating
val mathColor = careerMathSkill.color
是否可以使用数据类中的序列化数据将非序列化数据添加到同一个数据类中? (抱歉措辞奇怪,不知道怎么解释)
编辑:这是我所拥有的:
class CareersRemote(
@SerializedName("careers") val careers: List<Career>
) {
companion object {
fun parseResponse(response: Response<CareersRemote>): CareersResponse {
return if (response.isSuccessful) {
response.body()!!.format()
} else
CareersResponse(listOf(CareersResponse.ErrorType.Generic()))
}
}
fun format(): CareersResponse {
val careers = topCareers.map {
Career(
id = it.id,
title = it.title,
)
}.toMutableList()
return CareersResponse(CareersResponse.SuccessData(careers = careers))
}
data class Career(
@SerializedName("id") val id: String,
@SerializedName("title") val title: String,
)
}
这是我希望以某种方式做的事情
class CareersRemote(
@SerializedName("careers") val careers: List<Career>
) {
companion object {
fun parseResponse(response: Response<CareersRemote>): CareersResponse {
return if (response.isSuccessful) {
response.body()!!.format()
} else
CareersResponse(listOf(CareersResponse.ErrorType.Generic()))
}
}
fun format(): CareersResponse {
val careers = topCareers.map {
Career(
id = it.id,
title = it.title,
)
}.toMutableList()
return CareersResponse(CareersResponse.SuccessData(careers = careers))
}
data class Career(
@SerializedName("id") val id: String,
@SerializedName("title") val title: String,
// skills array that will need to be filled out based on the data I got in the json
var skills: List<Skill>
)
}
编辑:建议的解决方案
class CareersRemote(
@SerializedName("careers") val careers: List<Career>
) {
companion object {
fun parseResponse(response: Response<CareersRemote>): CareersResponse {
return if (response.isSuccessful) {
response.body()!!.format()
} else
CareersResponse(listOf(CareersResponse.ErrorType.Generic()))
}
}
fun format(): CareersResponse {
val careers = topCareers.map {
Career(
id = it.id,
title = it.title,
)
}.toMutableList()
return CareersResponse(CareersResponse.SuccessData(careers = careers))
}
data class Career(
@SerializedName("id") val id: String,
@SerializedName("title") val title: String,
@SerializedName("math_skill") val mathSkill: Int
@SerializedName("other_skill") val mathSkill: Int
) {
var skills: List<Skill> = {
val mathSkill = Skill(name: "Math", rating: mathSkill, color: /**some color*/)
val otherSkill = Skill(name: "Other", rating: otherSkill, color: /**some color*/)
return listOf(mathSkill, otherSkill)
}
}
}
【问题讨论】:
标签: android json serialization retrofit2