gts13
10/16/2022, 3:16 PM@Serializable
data class Accounts(
val data: List<Account>
) {
@Serializable
data class Account(
@SerialName("name")
val name: String,
@SerialName("lastname")
val lastname: String,
)
}
The Json.encodeToString()
and decodeToString
returns a JsonObject
{
"data": [
{
"name": "Mike",
"lastname": "Ekim"
},
{
"name": "John",
"lastname": "Nhoj"
},
...
How can encode the data class to return a JsonArray? Like the following:
data: [{
"name": "Mike",
"lastname": "Ekim"
}, {
"name": "John",
"lastname": "Nhoj"
},
...]
andylamax
10/16/2022, 9:39 PM@SerialName
if the json keys match your field names
Second.
You need a customer serializer that would ignore the data field in Accounts class and just return the list elements
Third.
If I were you, I wouldn't have an Accounts class, Just a bare Account class is enough to be used with Json.econdeToString<List<Account>>(listOf())
and Json.decodeToString<List<Account>>()