如何解析 jsonArray 我在获取 Json 数据时收到此错误
how to parse jsonArray i m getting this error , while fetching Json Data
运行时异常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"
}
我的数据Class
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,
)
我该怎么办?
错误告诉您响应实际上以 [
而不是 {
开头,所以我怀疑您实际上得到的是这些 Rides
对象的列表。
我看到你有
@GET("/rides")
suspend fun getAllRides() : RidesResponse
并且RidesResponse
是
data class RidesResponse(
val results : List<Rides>
)
现在你基本上是在告诉它响应应该是这样的
{
"results": [
...
]
}
但你没有。所以我认为您应该将 api 更改为
@GET("/rides")
suspend fun getAllRides() : List<Rides>
运行时异常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"
}
我的数据Class
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,
)
我该怎么办?
错误告诉您响应实际上以 [
而不是 {
开头,所以我怀疑您实际上得到的是这些 Rides
对象的列表。
我看到你有
@GET("/rides")
suspend fun getAllRides() : RidesResponse
并且RidesResponse
是
data class RidesResponse(
val results : List<Rides>
)
现在你基本上是在告诉它响应应该是这样的
{
"results": [
...
]
}
但你没有。所以我认为您应该将 api 更改为
@GET("/rides")
suspend fun getAllRides() : List<Rides>