我正在尝试在数据类中使用@Embedded,以便将子字段保存为我的房间数据库中的列。当我尝试运行我的应用程序时,出现此错误:
Expected BEGIN_OBJECT but was BEGIN_ARRAY
我的数据类如下所示:
@Entity(
tableName = "workorder",
indices = [Index("order_number"), Index("customer_number")]
)
data class WorkorderResponse(
@ColumnInfo(name = "acres")
val acres: Double?,
@PrimaryKey
@ColumnInfo(name = "order_number")
val orderNumber: Int,
...
@Embedded(prefix="application_")
val orderApplication: OrderApplication,
)
订单应用程序类如下所示:
data class OrderApplication(
val acres: String?,
val applicationMethod: String?,
val carrier: String?,
val cropHeight: Int?,
val currentPrecip: Int?,
val fieldConditions: String?,
val loadSiteLat: Int?,
val loadSiteLong: Int?,
val loadingMethod: String?,
val machine: String?,
val method: String?,
val nextDayPrecipAmount: Int?,
val nextDayPrecipChance: Int?,
val nextDayTemperature: Int?,
val nextDayWeather: String?,
val notes: String?,
val notified: String?,
val `operator`: Int?,
val pressure: String?,
val previousOrder: String?,
val reEntryInterval: Int?,
val rinsedPost: String?,
val rinsedPrevious: String?,
val rotateRest: String?,
val sensitiveArea: String?,
val sequence: Int?,
val soilConditions: String?,
val soilType: String?,
val temperature: Int?,
val tenderOperator: String?,
val timestampBegin: String?,
val timestampEnd: String?,
val tipSize: String?,
val weather: String?,
val weedHeight: Int?,
val windDirection: String?,
val windSpeed: Int?,
val wkt: String?
)
为了正确设置,我还需要做些什么吗?提前致谢!
我试过使用这样的列表:
@Embedded(prefix="application_")
val orderApplication: List<OrderApplication>,
但这给了我这个错误:
Entities and POJOs must have a usable public constructor.
另外,我有这些类型转换器:
@TypeConverter
fun fromOrderApplication(orderApplication: List<OrderApplication>): String{
val type = object : TypeToken<List<OrderApplication>>() {}.type
return gson.toJson(orderApplication, type)
}
@TypeConverter
fun toOrderApplication(json: String): List<OrderApplication> {
val type = object : TypeToken<List<OrderApplication>>() {}.type
return gson.fromJson(json, type)
}