運行時執行JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY"
這是我想要獲取的實際 json
{
"id": 414,
"origin_station_code": 5,
"station_path":[ 58,68,72,86],
"destination_station_code": 93,
"date": "03/12/2022 01:25 PM",
"map_url": "https://picsum.photos/200",
"state": "Arunachal Pradesh",
"city": "Pasighat"
}
我的資料類
data class Rides(
@SerializedName("id") var id: Int? = null,
@SerializedName("origin_station_code") var origin_station_code: Int? = null,
@SerializedName("station_path") var destination_station_code: Int? = null,
@SerializedName("destination_station_code") var station_path: ArrayList<Int> = arrayListOf(),
@SerializedName("date") var date: String? = null,
@SerializedName("map_url") var map_url: String? = null,
@SerializedName("state") var city: String? = null,
@SerializedName("city") var state: String? = null,
)
我該怎么辦 ?
uj5u.com熱心網友回復:
該錯誤告訴您回應實際上以[
而不是開始,{
因此我懷疑您實際上正在獲取這些Rides
物件的串列。
我看你有
@GET("/rides")
suspend fun getAllRides() : RidesResponse
并且RidesResponse
是
data class RidesResponse(
val results : List<Rides>
)
現在你基本上是在告訴它回應應該看起來像
{
"results": [
...
]
}
但你沒有那個。所以我認為您應該將api更改為
@GET("/rides")
suspend fun getAllRides() : List<Rides>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/444941.html