I'm trying to read the following data from the API:
[
[
1724180400000,
"59191.45000000",
"59597.28000000",
"59137.78000000",
"59511.99000000",
"544.56764000",
1724183999999,
"32351755.73378980",
51002,
"290.20494000",
"17241827.16012430",
"0"
],
[
1724184000000,
"59512.00000000",
"59722.72000000",
"59421.38000000",
"59589.07000000",
"301.58055000",
1724187599999,
"17968010.37382020",
23563,
"169.48375000",
"10099057.30721750",
"0"
]
]
But Retrofit expects a JSON response starting with '{' instead of '['.
I tried serializing the data with the following dependencies:
implementation(libs.retrofit)
implementation(libs.retrofit2.kotlinx.serialization.converter)
implementation(libs.okhttp)
implementation(libs.kotlinx.serialization.json)
When I tell Retrofit to expect a list with lists it states that 'Serializer for class 'Any' is not found'. How to serialize this response type with only arrays containing mixed types (String and Int)?
If type does not matter, you can just use type String
.
@GET("test")
fun test(): Call<List<List<String>>>
If type matters, you can use JsonPrimitive
.
@GET("test")
fun test(): Call<List<List<JsonPrimitive>>>
After you received data, you can parse it as List<List<Any>>
like this.
val result: List<List<JsonPrimitive>> = response.body()!!
val parsedResult: List<List<Any>> = result.map { innerItem ->
innerItem.map {
if (it.isNumber) it.asInt
else it.asString
}
}
println(parsedResult)