预计 BEGIN_ARRAY 但在第 1 行第 2 列 retrofit2 时 BEGIN_OBJECT

Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 retrofit2

我的项目有问题。它与 api 应用程序的连接很简单。我想获取这样的信息:

{
    "docs": [
        {
            "_id": "5cf5805fb53e011a64671582",
            "name": "The Fellowship Of The Ring"
        },
        {
            "_id": "5cf58077b53e011a64671583",
            "name": "The Two Towers"
        },
        {
            "_id": "5cf58080b53e011a64671584",
            "name": "The Return Of The King"
        }
    ],
    "total": 3,
    "limit": 1000,
    "offset": 0,
    "page": 1,
    "pages": 1
}

但是我得到了标题中的 illegalStateException。这是我的文件:

DTO

data class CharacterDTO(
    val docs: List<CharacterData>,
    val limit: Int,
    val offset: Int,
    val page: Int,
    val pages: Int,
    val total: Int
)
data class CharacterData(
    val _id: String,
    val birth: String,
    val death: String,
    val gender: String,
    val hair: String,
    val height: String,
    val name: String,
    val race: String,
    val realm: String,
    val spouse: String,
    val wikiUrl: String
)

fun CharacterData.toCharacter(): Character{
    return Character(
        _id = _id,
        birth = birth,
        death = death,
        gender = gender,
        name = name,
        race = race
    )
}

它在这个文件上崩溃了: 用例

lass GetCharactersUseCase @Inject constructor(
    private val repository: LotrRepository
) {
    operator fun invoke(): Flow<List<Character>> = flow {
        try {
            val characters = repository.getCharacters() // on this line it crash
            Log.d(Tag,characters.toString())
        }catch (e: HttpException){
            Log.d(Tag,e.toString())
        }catch (e: IOException){
            Log.d(Tag,e.toString())
        }
    }
}

这是存储库

interface LotrRepository {

    suspend fun getBooks(): List<BookDTO>

    suspend fun getCharacters(): List<CharacterDTO>

    suspend fun getCharacterById(characterId: String): CharacterDTO
}

最后这是我的要求:

    @Provides
    @Singleton
    fun provideLotrApi(): LotrApi{
        val request = Retrofit.Builder()
            .client(OkHttpClient.Builder().addInterceptor { chain ->
                val request = chain.request().newBuilder().addHeader("Authorization", "Bearer ${Constants.API_KEY}").build()
                chain.proceed(request)
            }.build())
            .baseUrl(Constants.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
            .create(LotrApi::class.java)
        return request
    }

经过我的研究我忘了处理这部分

    "total": 3,
    "limit": 1000,
    "offset": 0,
    "page": 1,
    "pages": 1

但是在更改我的 DTO 文件后它仍然不起作用

上面提供的

JSON 仅包含一个 CharacterDTO 对象,但您希望获得此类对象的列表。将后端更改为 return 列表或将 getCharacters() 声明替换为:

suspend fun getCharacters(): CharacterDTO