I am using retrofit2 kotlinx serialization to deserialize the response. The json response look like something like this.
{
"type": "ETA",
"version": "1.0",
"generated_timestamp": "2022-11-29T15:39:57 08:00",
"data": [
{
"co": "KMB",
"route": "91M",
"dir": "I",
"service_type": 1,
"seq": 13,
"dest_tc": "寶林",
"dest_sc": "宝林",
"dest_en": "PO LAM",
"eta_seq": 1,
"eta": "2022-11-29T15:52:21 08:00",
"rmk_tc": "",
"rmk_sc": "",
"rmk_en": "",
"data_timestamp": "2022-11-29T15:39:44 08:00"
},
{
"co": "KMB",
"route": "91M",
"dir": "I",
"service_type": 1,
"seq": 13,
"dest_tc": "寶林",
"dest_sc": "宝林",
"dest_en": "PO LAM",
"eta_seq": 2,
"eta": "2022-11-29T16:06:39 08:00",
"rmk_tc": "原定班次",
"rmk_sc": "原定班次",
"rmk_en": "Scheduled Bus",
"data_timestamp": "2022-11-29T15:39:44 08:00"
},
{
"co": "KMB",
"route": "91M",
"dir": "I",
"service_type": 1,
"seq": 13,
"dest_tc": "寶林",
"dest_sc": "宝林",
"dest_en": "PO LAM",
"eta_seq": 3,
"eta": "2022-11-29T16:21:39 08:00",
"rmk_tc": "原定班次",
"rmk_sc": "原定班次",
"rmk_en": "Scheduled Bus",
"data_timestamp": "2022-11-29T15:39:44 08:00"
}
]
}
I try parsing with this model.
@Serializable
data class MarsPhoto(
val type: String = "", //The corresponding API that returns the data
val version: String = "",//The version number of the JSON returned.
val generated_timestamp: String = "",//The timestamp of the initial generated time of the response before it is cached.
val data: Eta
)
@Serializable
data class Eta(
val co:String ="KMB",//The bus company
val route: String = "", //The bus route number of the requested bus company
val dir: String = "",//The direction of the bus route
val service_type: String = "",//The service type of the bus route.
val seq: Int = 0, //The stop sequence number of a bus route
val dest_tc: String = "",//The destination of a bus route in Traditional Chinese
val dest_sc: String = "",//The destination of a bus route in Simplified Chinese.
val dest_en: String = "",//The destination of a bus route in English
val eta_seq: Int = 0,//The sequence number of ETA
val eta: String = "-",//The timestamp of the next ETA
val rmk_tc: String = "",//The remark of an ETA in Traditional Chinese
val rmk_sc: String = "",//The remark of an ETA in Simplified Chinese.
val rmk_en: String = "",//The remark of an ETA in English
val data_timestamp: String = "",//The timestamp of the data when it was initially
)
but an exception occur!
FATAL EXCEPTION: main Process: com.example.marsphotos, PID: 17692 kotlinx.serialization.json.internal.JsonDecodingException: Expected start of the array '[', but had 'EOF' instead at path: $
JSON input: .....heduled Bus","data_timestamp":"2022-11-29T16:30:16 08:00"}]}
May i get some help
CodePudding user response:
From what I see in your json response, you receive an array of Eta objects.
So, you may try to rewrite your classes to:
@Serializable
data class MarsPhoto(
val type: String = "", //The corresponding API that returns the data
val version: String = "",//The version number of the JSON returned.
val generated_timestamp: String = "",//The timestamp of the initial generated time of the response before it is cached.
val data: Data
)
@Serializable
data class Eta(
val co:String ="KMB",//The bus company
val route: String = "", //The bus route number of the requested bus company
val dir: String = "",//The direction of the bus route
val service_type: String = "",//The service type of the bus route.
val seq: Int = 0, //The stop sequence number of a bus route
val dest_tc: String = "",//The destination of a bus route in Traditional Chinese
val dest_sc: String = "",//The destination of a bus route in Simplified Chinese.
val dest_en: String = "",//The destination of a bus route in English
val eta_seq: Int = 0,//The sequence number of ETA
val eta: String = "-",//The timestamp of the next ETA
val rmk_tc: String = "",//The remark of an ETA in Traditional Chinese
val rmk_sc: String = "",//The remark of an ETA in Simplified Chinese.
val rmk_en: String = "",//The remark of an ETA in English
val data_timestamp: String = "",//The timestamp of the data when it was initially
)
@Serializable
data class Data( val data: List<Eta>)
CodePudding user response:
Try this
@Serializable
data class MarsPhoto(
val type: String = "",
val version: String = "",
val generated_timestamp: String = "",
val data: List<Eta> = emptyList()
)