Kotlin 中特定代码的问题:预期 BEGIN_OBJECT 但在第 1 行第 75 列路径 $.data 处 BEGIN_ARRAY
Problem with specific code in Kotlin : Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 75 path $.data
我现在有一个问题。
我正在尝试从 api 休息网站 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>
}
MainActivity
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"}
提供的 JSON data
字段是 JSON 数组,而不是 JSON 对象,因此您需要更改
var data: Data? = null
类似于:
var data: List<Data>? = null
我现在有一个问题。 我正在尝试从 api 休息网站 Url 获取数据,我已经完成了我需要做的所有事情,但它不起作用。 我不知道这是什么问题,谢谢帮助我 错误:预期 BEGIN_OBJECT 但在第 1 行第 75 列路径 $.data
BEGIN_ARRAYAllClass.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>
}
MainActivity
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"}
提供的 JSON data
字段是 JSON 数组,而不是 JSON 对象,因此您需要更改
var data: Data? = null
类似于:
var data: List<Data>? = null