I'm looking to parse the below JSON:
{
"list": [
{
"data1": "data1",
"transaction": {
"data2": "data2",
"data3": "data3"
},
"breakdowns": [
{
"data4": "data4",
"data5": "data5"
}
]
}
]
}
I'm using Moshi and okHttpClient to handle this JSON. My data class is correct
But when I try to parse it as below:
val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.build()
val type = Types.newParameterizedType(List::class.java,PaymentRequest::class.java)
try{
val q = moshi.adapter(type)
paymentRequest = q.fromJson(response.body!!.source())!!
} catch (e: Exception) {
println(e)
}
I get this error : com.squareup.moshi.JsonDataException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at path $
You can't treat this json as a list. It's not a list itself but actually is a json object that contains a list.
To solve that, first build a class to wrap the
"list"
:Then you are good to go: