kotlin - 预期为 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
}
}
}
有人可以帮忙吗?如果您需要更多信息,我可以提供。
非常感谢您的友好回复。
问候。
解决方案
推荐阅读
- php - 无法更新 MySQL 数据库?
- apache-spark - Spark Shuffle 分区 - 如果我的 shuffle 分区少于核心数会发生什么?
- android - 这是什么意思 download_url = task.getResult().getStorage().getDownloadUrl().toString()
- javascript - Object.defineProperty 无法识别 getter 或 setter 语法
- javascript - 获取 div 内元素(子元素)的 ID 和内部文本
- python - 如何使用 NumPy 将 uint32 解释为 float32?
- python - 如何使用 minAreaRect() 函数而不出错?
- java - 从正则表达式中获取变量
- java - 有效性的 Java 字符串操作
- uml - uml - 节点边缘关系的类图