2

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

1
  • Why you want to read Times as a list while it's an object! Commented Aug 7, 2019 at 23:54

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.