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

Time:03-16

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 ?

CodePudding user response:

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>
  • Related