首页 > 解决方案 > 预期为 BEGIN_OBJECT,但在第 1 行第 2 列路径为 BEGIN_ARRAY $ ERROR

问题描述

感谢您的帮助,第一次写。

我正面临这个问题,我正在使用 Kotling 和 Retrofit。

[ {
"name": "Bulbasaur",
"id": "#001",
"imageurl": "https://assets.pokemon.com/assets/cms2/img/pokedex/full/001.png",
"xdescription": "Bulbasaur can be seen napping in bright sunlight. There is a seed on its back. By soaking up the sun's rays, the seed grows progressively larger.",
"ydescription": "Bulbasaur can be seen napping in bright sunlight. There is a seed on its back. By soaking up the sun's rays, the seed grows progressively larger.",
"height": "2' 04\"",
"category": "Seed",
"weight": "15.2 lbs",
"typeofpokemon": ["Grass", "Poison"],
"weaknesses": ["Fire", "Flying", "Ice", "Psychic"],
"evolutions": ["#001", "#002", "#003"],
"abilities": ["Overgrow"],
"hp": 20,
"attack": 30,
"defense": 20,
"special_attack": 30,
"special_defense": 30,
"speed": 30,
"total": 160,
"male_percentage": "87.5%",
"female_percentage": "12.5%",
"genderless": 0,
"cycles": "20 ",
"egg_groups": "Grass, Monster ",
"evolvedfrom": "",
"reason": "",
"ase_exp": "64"}]

我知道这意味着这是一个列表,我想我必须转换部分数据类来修复它,但不知道该怎么做。

这是我的课:

data class PokemonAttributes(val name: String, val id: String, val imageurl: String, val xdescription: String)

data class PokemonResponse(val results: MutableList<PokemonAttributes>)

    

object RetrofitClient {
    fun getRetrofit(): Retrofit {
        var okHttpClient =
            OkHttpClient().newBuilder()
                .addInterceptor(HttpLoggingInterceptor().apply {
                    level =
                        if (BuildConfig.DEBUG) HttpLoggingInterceptor.Level.BODY
                        else HttpLoggingInterceptor.Level.NONE
                })
                .build()


        return Retrofit.Builder()
            .baseUrl("https://gist.githubusercontent.com/mrcsxsiq/b94dbe9ab67147b642baa9109ce16e44/raw/97811a5df2df7304b5bc4fbb9ee018a0339b8a38/")
            .client(okHttpClient)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    }
}

interface PokeAPI {

    //Ponemos punto para que la base URL sea la misma
    @GET(".")
    suspend fun getPokemons() : Response<PokemonResponse>
}

object Repository {
    suspend fun getAttributes(): PokemonResponse? {
        return try {
            val call = RetrofitClient.getRetrofit().create(PokeAPI::class.java).getPokemons()
            call.body()
        } catch (e: Exception) {
            e.log()
            null
        }
    }
}

有人可以帮忙吗?如果您需要更多信息,我可以提供。

非常感谢您的友好回复。

问候。

标签: kotlinretrofit

解决方案


推荐阅读