Kotlin中的特定代码出现问题:应为BEGIN_OBJECT,但在第1行第75列路径$.data处为BEGIN_ARR



我现在有个问题。我正试图从一个api rest网站Url中获取数据,我已经完成了所有需要做的事情,但它不起作用。我不知道这个问题是什么,谢谢你帮我错误:应为BEGIN_OBJECT,但在第1行第75列路径$.data 处为BEGIN_ARRAY

AllClass.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
}

ClassValApi.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":"Guineau2019s 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"}

在提供的JSONdata字段是JSONArray,而不是JSONObject,因此需要更改

var data: Data? = null

类似于:

var data: List<Data>? = null

相关内容

最新更新