'Only get parameter: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ [RetroFit]

Hello I have the following response and I want to get only userList as my response using Retrofit

{
    "returnCode": 0,
    "userList": [
        {
            "id": 1,
            "name": "John",
        },
        {
            "id": 2,
            "name": "Paul",
        },
        {
            "id": 3,
            "name": "George",
        },
        {
            "id": 4,
            "name": "Ringo",
        }
    ]
}

If I try my response as a list I get this error:

Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

And has sense for me because I'm getting an object instead an array, but I think it's useless create and object who contains a list and a returnCode parameter.

How can I get my list using Retrofit?

This is the way that I'm doing my response treatment:

suspend fun getUsers(): List<User>{
        return withContext(Dispatchers.IO){
            val response = retrofit.create(GetUserApiClient::class.java).getUsers()
            print(response)
            response.body() ?: emptyList()
       }
}


interface GetUserApiClient {
    @POST("/app/getUsers")
    suspend fun getUsers(): Response<List<User>?>
}

Thnx a lot!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source