gson Kotlin中特定代码的问题:预期为开始_OBJECT,但在第1行第75列路径$.data处为BEGIN_ARRAY

gcmastyq  于 2022-11-06  发布在  Kotlin
关注(0)|答案(1)|浏览(209)

我有一个问题我现在。我试图从一个api休息网站网址获取数据,我已经做了所有的事情我需要做,但它不工作。我不知道这是什么问题,谢谢你帮助我错误:预期为开始_OBJECT,但在第1行第75列路径$.data处为BEGIN_ARRAY

所有类.kt

import com.google.gson.annotations.Expose
import com.google.gson.annotations.SerializedName

class ClassVal {
    @SerializedName("pagination")
    @Expose
    var pagination: Pagination? = null

    @SerializedName("data")
    @Expose
    var data: Data? = null
}

class Pagination {
    @SerializedName("limit")
    @Expose
    var limit: Int? = null

    @SerializedName("count")
    @Expose
    var count: Int? = null
}

class Data {
    @SerializedName("author")
    @Expose
    val author: String? = null

    @SerializedName("title")
    @Expose
    val title: String? = null

    @SerializedName("description")
    @Expose
    val description: String? = null

    @SerializedName("url")
    @Expose
    val url: String? = null

    @SerializedName("source")
    @Expose
    val source: String? = null

    @SerializedName("image")
    @Expose
    val image: String? = null

    @SerializedName("category")
    @Expose
    val category: String? = null

    @SerializedName("language")
    @Expose
    val language: String? = null

    @SerializedName("country")
    @Expose
    val country: String? = null

    @SerializedName("published_at")
    @Expose
    val published: String? = null
}

类值应用程序.kt

import retrofit2.Call
import retrofit2.http.GET

interface ClassValApi {

    @GET("v1/news?access_key=EnterKey")
    fun getNews() : Call<ClassVal>

}

主要活动

fun getClassValApi() {
    val retro = Retro().getRetroClientInstance().create(ClassValApi::class.java)
    retro.getNews().enqueue(object : Callback<ClassVal> {
        override fun onResponse(call: Call<ClassVal>, response: Response<ClassVal>) {
            val news = response.body().toString()
                Log.i("bon voila", news)
        }

        override fun onFailure(call: Call<ClassVal>, t: Throwable) {
            Log.e("Failed", t.message.toString())
        }

    })
}

Json代码示例

Yes i have an exemple : {"pagination":                
{"limit":25,"offset":0,"count":25,"total":13339023},"data": 
[{"author":"Reuters","title":"Guinea\u2019s Conde wins presidency with 59.5% 
of vote -election commission","description":"CONAKRY \u0026#8212; President 
Alpha Conde of Guinea won the Oct. 18 election with 59.5% of the vote, 
according to a full preliminary tally from the election commission on 
Saturday. The victory, which requires confirmation by the Constitutional 
Court, gives a third term in office to the 82-year-old Conde after a bitterly 
fought election in which[\u0026#8230;]","url":"https:\/\/nationalpost.com\/pmn\/news-pmn\/guineas-conde-wins-presidency-with-59-5-of-vote-election-commission","source":"nationalpost","image":null,"category":"general","language":"en","country":"us","published_at":"2020-10-24T13:53:58+00:00"}
qacovj5a

qacovj5a1#

在所提供的JSON中,data字段是一个JSONArray,而不是一个JSONObject,因此需要将

var data: Data? = null

转换为类似以下的内容:

var data: List<Data>? = null

相关问题