I'm trying to parse newline delimited json using retrofit and moshi. This is my GET function:
suspend fun getDeviceValuesNew(@Path("application-id") applicationId: String, @Path("device-id") deviceId: String)
: Response<List<ValueApiResponse>>
When I try to run it, I get this error:
com.squareup.moshi.JsonDataException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at path $
The HTTP call returns json like this:
{
"result": {
"end_device_ids": {
"device_id": "esp32",
"application_ids": {}
},
"received_at": "2021-03-31T11:33:42.757281753Z",
"uplink_message": {
"decoded_payload": {
"brightness": 0
},
"settings": {
"data_rate": {}
},
"received_at": "2021-03-31T11:33:42.547285090Z"
}
}
}
{
"result": {
"end_device_ids": {
"device_id": "esp32",
"application_ids": {}
},
"received_at": "2021-03-31T11:18:17.745921472Z",
"uplink_message": {
"decoded_payload": {
"brightness": 0
},
"settings": {
"data_rate": {}
},
"received_at": "2021-03-31T11:18:17.538276218Z"
}
}
}
EDIT #1: As you can see in my answer below, I managed to get a valid JSON response from the API, but still I'm struggling to parse these JSON objects to a list of Kotlin objects. How do I get Moshi to handle these newline delimited JSON objects as a list? I think the problem is that Moshi requires the objects to be wrapped inside an array to be recognised as a list. How do I do that?
This is my data class used for parsing:
@JsonClass(generateAdapter = true)
data class ValueDto(
@Json(name = "result")
val result: Result
) {
@JsonClass(generateAdapter = true)
data class Result(
@Json(name = "end_device_ids")
val endDeviceIds: EndDeviceIds,
@Json(name = "received_at")
val receivedAt: String,
@Json(name = "uplink_message")
val uplinkMessage: UplinkMessage
) {
@JsonClass(generateAdapter = true)
data class EndDeviceIds(
@Json(name = "application_ids")
val applicationIds: ApplicationIds,
@Json(name = "device_id")
val deviceId: String
) {
@JsonClass(generateAdapter = true)
class ApplicationIds(
)
}
@JsonClass(generateAdapter = true)
data class UplinkMessage(
@Json(name = "decoded_payload")
val decodedPayload: DecodedPayload,
@Json(name = "received_at")
val receivedAt: String,
@Json(name = "settings")
val settings: Settings
) {
@JsonClass(generateAdapter = true)
data class DecodedPayload(
@Json(name = "brightness")
val brightness: Int
)
@JsonClass(generateAdapter = true)
data class Settings(
@Json(name = "data_rate")
val dataRate: DataRate
) {
@JsonClass(generateAdapter = true)
class DataRate(
)
}
}
}
}