简体   繁体   中英

how to parse jsonArray i m getting this error , while fetching Json Data

Execption at Runtime JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY"

This is Actual json i want to fetch

{
    "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"
}

My Data 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,

)

What should i do?

The error tells you that the response actually begins with [ instead of { so I suspect that you are actually getting a list of these Rides objects.

I see you have

@GET("/rides")
suspend fun getAllRides() : RidesResponse

and RidesResponse is

data class RidesResponse(
    val results : List<Rides>
)

Now you basically are telling it that the respons should look like

{
    "results": [
      ...
    ]
}

but you don't have that. So I think you should just change the api to

@GET("/rides")
suspend fun getAllRides() : List<Rides>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM