Reputation: 431
I have a json object returning from a server which needs to be converted to a list
{"name":"xyz","times":{"x1":12.34,"x2":1.9609,"x3":4.8042}}
In the above json, times is a json object. I convert it to data class as below
@Serializable
data class ConversionRates(
val x1: String,
val time: Times
)
@Serializable
data class Times(val x1: Double, val x2: Double, val x3: Double)
But, I want Times as an listOf(String, Double) rather than as an object.
I have tried to use KSerializers but ran out of luck
Upvotes: 2
Views: 2990
Reputation: 639
I'm sure you can use GSON
and a Map<String, Double>
data class ConversionRates(val name: String, val times: Map<String, Double>)
...
val conversionRates = GSON.fromJson(jsonString, ConversionRates::class.java)
val listRates = conversionRates.times.values
Upvotes: 3