简体   繁体   English

如何在房间数据库中使用 JSONObject Android

[英]How to use JSONObject in Room Database Android

I am having an issue if I take JSONObject in room database entity.如果我在房间数据库实体中使用 JSONObject,我会遇到问题。

@Entity(tableName = RoomDbConstant.formTable)
data class FormEntity(
    @NonNull
    @SerializedName("id")
    @PrimaryKey(autoGenerate = false)
    var formId: String,
    @NonNull
    @SerializedName("title")
    var formTitle: String?,
    @NonNull
    var formCategoryId: String?,
    @NonNull
    var formSubCategoryId: String?,
    @NonNull
    var formData: String?,
    @NonNull
    var is_active: String? = AppConstants.ACTIVE,
    @NonNull
    var status: String? = AppConstants.NONE,
    @NonNull
    var is_sent_server: String? = AppConstants.NOT_SENT,
    @NonNull
    var tempEntryExisted: Boolean? = false,
    @NonNull
    @Embedded(prefix="FormDataEntity")
    var formDataEntity: FormDataEntity?,
) : Comparable<FormEntity?> {
    override fun compareTo(other: FormEntity?): Int {
        return formTitle?.compareTo(other?.formTitle.toString(), ignoreCase = true)!!
    }
}

data class FormDataEntity(
    @Embedded
    @NonNull
    var response: FormFieldsEntity
)

data class FormFieldsEntity(
    @NonNull
    var is_active: String = AppConstants.ACTIVE,
    @NonNull
    @Embedded
    var fields: ArrayList<FieldEntity>,
    @NonNull
    @SerializedName("title")
    var formTitle: String,
    @NonNull
    @SerializedName("id")
    var formId: String,
    @NonNull
    @SerializedName("gfpdf_form_settings")
    var gfpdfFormSettings: JSONObject,
)

I have three classes First class is FormEntity which has sub-class FormDataEntity and that FormDataEntity class has a subclass that has JSONObject.我有三个类首先 class 是 FormEntity,它有子类 FormDataEntity,而 FormDataEntity class 有一个有 JSONObject 的子类。 SO I am getting data from api in which there is one JSONObject with the name "gfpdf_form_settings".所以我从 api 获取数据,其中有一个名为“gfpdf_form_settings”的 JSONObject。 enter image description here在此处输入图像描述

So I am adding data in room databse when fetching data by converting using GSON from api.所以我在通过使用 api 转换为 GSON 获取数据时在房间数据库中添加数据。

val data: FormDataEntity = Gson().fromJson(obj, FormDataEntity::class.java)
                    val formModel = FormEntity(formId.toString(),Utilities.separateTitle(data.response.formTitle)[0].toString()
                        ,mCatId,mSubCatId.toString(),obj,data.response.is_active,
                        AppConstants.NONE,AppConstants.NOT_SENT,false,data)
But not able to do because I get error of roomdatabase.

Engineering Forms/Version 2 Room database offline functionality/EngineeringForm/app/build/tmp/kapt3/stubs/debug/com/app/engineeringform/controller/database/entities/formEntities/FormFieldsEntity.java:20: error: Cannot figure out how to save this field into database.工程表格/版本 2 房间数据库离线功能/EngineeringForm/app/build/tmp/kapt3/stubs/debug/com/app/engineeringform/controller/database/entities/formEntities/FormFieldsEntity.java:20: error: 无法弄清楚如何将该字段保存到数据库中。 You can consider adding a type converter for it.您可以考虑为其添加类型转换器。 private org.json.JSONObject gfpdfFormSettings;私有 org.json.JSONObject gfpdfFormSettings; ^[WARN] Incremental annotation processing requested, but support is disabled because the following processors are not incremental: androidx.room.RoomProcessor (DYNAMIC). ^[WARN] 已请求增量注释处理,但支持被禁用,因为以下处理器不是增量的:androidx.room.RoomProcessor(动态)。

I have tried every way but not able to find solution how to use JSONObject in entity class.我已经尝试了各种方法,但无法找到如何在实体 class 中使用 JSONObject 的解决方案。

You cannot save a JSONObject as entity in roomdb.您不能将JSONObject保存为 roomdb 中的实体。 In this case, you may copy the entire gfpdfForm JSON into JSON into Kotlin data class plugin to make unique data class for it.在这种情况下,您可以将整个gfpdfForm JSON into JSON into Kotlin data class插件复制为唯一数据class。

You may reference here: https://plugins.jetbrains.com/plugin/9960-json-to-kotlin-class-jsontokotlinclass-您可以在这里参考: https://plugins.jetbrains.com/plugin/9960-json-to-kotlin-class-jsontokotlinclass-

There are many answer on converting JSON into data class关于将 JSON 转换为数据 class 有很多答案

  1. insert JSON data using Room Library 使用 Room Library 插入 JSON 数据
  2. Android Room: Efficient way to transform json result into db object Android 房间:将 json 结果转换为 db object 的有效方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM