Expected begin_array but was begin_object at line 1 column 2 path $ on Kotlin+Android

huangapple go评论80阅读模式
英文:

Expected begin_array but was begin_object at line 1 column 2 path $ on Kotlin+Android

问题

我调用了PetFinder API来获取数据。当我想要在屏幕上显示数据时,出现了这个错误。我使用了调用顺序来获取数据,并且在ViewModel上使用了enqueue。

我尝试使用fromJson()方法,但代码给了我错误。

英文:

I called the PetFinder API to get data. When I wanted to show the data on screen I got this error. I used Call sequence to get data and I used enqueue on the ViewModel.

I tried to use fromJson() method but the code gave me error

答案1

得分: 0

SOLUTION:

我的数据类是

data class Animal(
    @SerializedName("name")
    val name: String,
    @SerializedName("age")
    val age: String,
    @SerializedName("gender")
    val gender: String
)

当我使用enqueue方法时,我使用了Animal。我创建了另一个数据类:

data class Deneme(
    val animals: List<Animal>
)

然后我使用Deneme类替代了Animal

repository.getData().enqueue(object : Callback<Deneme> {
英文:

SOLUTION:

My data class was

data class Animal(
@SerializedName(&quot;name&quot;)
val name:String,
@SerializedName(&quot;age&quot;)
val age: String,
@SerializedName(&quot;gender&quot;)
val gender:String,
)

and I used Animal when I use enqueue method. I created another data class :

data class Deneme(
    val animals: List&lt;Animal&gt;,
)

and I used Deneme class instead of Animal

    repository.getData().enqueue(object : Callback&lt;Deneme&gt; {

huangapple
  • 本文由 发表于 2023年8月9日 14:20:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76865063-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定