Vivek Modi
07/21/2022, 11:05 AM{
"TopicOne": [
{
"id": 1,
"seconds": 5.856,
"date": "21/10/2010"
},
.... // more data
],
"TopicTwo": [
{
"gameNumber": 6,
"name": "Heat",
"date": "21/11/2010"
},
..... // more data
],
"TopicThree": [
{
"id": 1,
"numberOfSets": 3,
"date": "21/11/2011",
},
....// more data
]
}
and see more in thread..Vivek Modi
07/21/2022, 11:05 AMdata class TopicResponse(
val topicOne: List<TopicOne>? = null,
val topicTwo: List<TopicTwo>? = null,
val topicThree: List<TopicThree>? = null
)
data class TopicOne(val id: Int? = null, val seconds: String? = null, val date: String? = null)
data class TopicTwo(val gameNumber: Int? = null, val name: String? = null, val date: String? = null)
data class TopicThree(val id: Int? = null, val numberOfSets: String? = null, val date: String? = null)
Vivek Modi
07/21/2022, 11:05 AMdate
is common so created a base class
@Serializable
abstract class TopicBase {
abstract val date: String
}
Vivek Modi
07/21/2022, 11:06 AM@Serializable
class ChildTopicOne(override val date: String, private val id: Int, private val seconds: String?) : TopicBase() {
fun getString() :String {
return "Id $id has to complete the race in $seconds seconds"
}
}
@Serializable
class ChildTopicTwo(override val date: String, val gameNumber: Int, val name: String?) : TopicBase() {
fun getString() :String {
return "gameNumber $gameNumber has to complete the race in $name name"
}
}
@Serializable
class ChildTopicThree(override val date: String, val id: Int, val numberOfSets: String?) : TopicBase() {
fun getString() :String {
return "id $id has to complete the race in $numberOfSets numberOfSets"
}
}
Vivek Modi
07/21/2022, 11:06 AMresponse.topicOne?.forEach { topicOne ->
topicOne.date?.let {
val value: TopicBase = ChildTopicOne(it, topicOne.id, topicOne.seconds)
val data = Json.encodeToJsonElement(value)
Log.e("data", "${data}")
}
}
Vivek Modi
07/21/2022, 11:06 AM{
"type": "com.vivek.example.data.ChildTopicOne",
"date": "21/10/2010",
"seconds": "5.856",
"id": "1"
}
Expected Output
{
"type": "com.vivek.example.data.ChildTopicOne",
"getString": "Id 1 has to complete the race in 5.856 seconds"
}
Is it possible to get this type of output?Vivek Modi
07/21/2022, 11:14 AM