JsonArray to Kotlin data class using Retrofit (Expected BEGIN_OBJECT but was BEGIN_ARRAY)

Viewed 4313

I'm using Retrofit2

fun create(): MyApiService {

    return Retrofit.Builder()
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(BASE_URL)
                .build()
                .create(MyApiService::class.java)
}

to implicitly convert the following Json

[
    {
        "id": 1,
        "name": "John",
    }, {
        "id": 2,
        "name": "Mary",
    }
]

into a Kotlin data class

object Model {
    data class Person(val id: Int, val name: String)
}

However, I'm getting the Expected BEGIN_OBJECT but was BEGIN_ARRAY error when attempting to

@GET("/people.json")
fun getPeople() : Observable<Model.Person>

I've tried changing the Model object to extend from List (as you'd normally do in Retrofit 1 with Java) or creating a List field of people, but to no avail.

1 Answers
Related