我使用retrofit 2 + kotlinx序列化来反序列化响应。json响应看起来像这样。
{
"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"
}
]
}
我尝试用这个模型进行解析。
@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
)
但出现异常!
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"}]}
我可以得到一些帮助吗
2条答案
按热度按时间9udxz4iz1#
从我在json响应中看到的内容来看,您收到了一个Eta对象数组。
因此,您可以尝试重写您的类以:
6uxekuva2#
试试这个