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)?
2
Answers
The answers in this thread helped a lot to understand and solve the problem. In my Android app I implemented the solution like this:
In the api service (Retrofit 2 interface)
In the network repository, fetch and convert the rawJson to my custom Kline class
The
Call
type was not needed in my scenario because the functions are called from within a coroutine scope.The rawJson result in logcat:
This implementation works for me, but better solutions are appreciated!
If type does not matter, you can just use type
String
.If type matters, you can use
JsonPrimitive
.After you received data, you can parse it as
List<List<Any>>
like this.