如何使用 Moshi 解析从 Arraylist 扩展的类

问题描述 投票:0回答:0

我是 Moshi 的新手,我正在尝试使用 Retrofit 从一个扩展项目数组列表的类中解析数据,但出现此错误:

  com.squareup.moshi.JsonDataException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at path $

班级:

class CarSelection: ArrayList<CarItem>()

data class CarItem(
    @field:Json(name = "type") val type: String,
    @field:Json(name = "name") val name: String,
    @field:Json(name = "color") val color: String,
    @field:Json(name = "image") val image: String
)

改造服务:

 fun getRemoteService(baseUrl :String, client : OkHttpClient): MyRemoteService {
            return Retrofit.Builder()
                .baseUrl(baseUrl)
                .client(client)
                .addConverterFactory(MoshiConverterFactory.create())
                .build()
                .create(MyRemoteService::class.java)
        }

提前致谢

android retrofit2 moshi data-moshing
© www.soinside.com 2019 - 2024. All rights reserved.