当前位置: 首页 > 知识库问答 >
问题:

Kotlin需要BEGIN_OBJECT,但BEGIN_ARRAY位于第1行第2列路径$

宗政学
2023-03-14

这是我使用插件kotlin数据类的模型

data class CatsItem(
    @SerializedName("adaptability")
    val adaptability: Int,
    @SerializedName("affection_level")
    val affectionLevel: Int,
    @SerializedName("alt_names")
    val altNames: String,
    @SerializedName("bidability")
    val bidability: Int,
    @SerializedName("cat_friendly")
    val catFriendly: Int,
    @SerializedName("cfa_url")
    val cfaUrl: String,
    @SerializedName("child_friendly")
    val childFriendly: Int,
    @SerializedName("country_code")
    val countryCode: String,
    @SerializedName("country_codes")
    val countryCodes: String,
    @SerializedName("description")
    val description: String,
    @SerializedName("dog_friendly")
    val dogFriendly: Int,
    @SerializedName("energy_level")
    val energyLevel: Int,
    @SerializedName("experimental")
    val experimental: Int,
    @SerializedName("grooming")
    val grooming: Int,
    @SerializedName("hairless")
    val hairless: Int,
    @SerializedName("health_issues")
    val healthIssues: Int,
    @SerializedName("hypoallergenic")
    val hypoallergenic: Int,
    @SerializedName("id")
    val id: String,
    @SerializedName("indoor")
    val indoor: Int,
    @SerializedName("intelligence")
    val intelligence: Int,
    @SerializedName("lap")
    val lap: Int,
    @SerializedName("life_span")
    val lifeSpan: String,
    @SerializedName("name")
    val name: String,
    @SerializedName("natural")
    val natural: Int,
    @SerializedName("origin")
    val origin: String,
    @SerializedName("rare")
    val rare: Int,
    @SerializedName("rex")
    val rex: Int,
    @SerializedName("shedding_level")
    val sheddingLevel: Int,
    @SerializedName("short_legs")
    val shortLegs: Int,
    @SerializedName("social_needs")
    val socialNeeds: Int,
    @SerializedName("stranger_friendly")
    val strangerFriendly: Int,
    @SerializedName("suppressed_tail")
    val suppressedTail: Int,
    @SerializedName("temperament")
    val temperament: String,
    @SerializedName("vcahospitals_url")
    val vcahospitalsUrl: String,
    @SerializedName("vetstreet_url")
    val vetstreetUrl: String,
    @SerializedName("vocalisation")
    val vocalisation: Int,
    @SerializedName("weight")
    val weight: Weight,
    @SerializedName("wikipedia_url")
    val wikipediaUrl: String
)

这是我的回应:

>  viewModel.myResponse.observe(this, Observer { response ->
            if (response.isSuccessful) {
                Log.d("Response", response.body()?.id.toString())
               massage.text = response.body()?.name.toString()
                

            } else {
                Log.d("Response", response.errorBody().toString())
                errormassage.text = response.code().toString()
            }
        })

我想在textView中显示品种名称,但运行应用程序时出错:

共有1个答案

谭越
2023-03-14

从您指定的URL得到的JSON返回一个对象数组。如果整个响应是成功案例中response.body()中的内容,那么您的代码没有正确处理该响应。假设返回的结构由单个对象而不是对象数组组成。

换句话说,回来的结果是这样的:

[
    {
        "key1": "value1",
        "key2": 1234
    }
]

但您的代码需要:

{
    "key1": "value1",
    "key2": 1234
}
response.body()?.get(0).name.toString()
 类似资料: