在 kotlin 中从 JsonArray/DataModel class 创建房间数据库 "Entity" class 的问题

Problem to create room database's "Entity" class from JsonArray/DataModel class in kotlin

我是房间数据库的新手。我在从 JsonArray.

下面创建实体 class 时遇到问题

你能帮助从以下文件创建实体 class:

[
{
"id":1,
"name":"Leanne Graham",
"username":"Bret",
"email":"Sincere@april.biz",
"address":{
"street":"Kulas Light",
"suite":"Apt. 556",
"city":"Gwenborough",
"zipcode":"92998-3874",
"geo":{
"lat":"-37.3159",
"lng":"81.1496"
}
},
"phone":"1-770-736-8031 x56442",
"website":"hildegard.org",
"company":{
"name":"Romaguera-Crona",
"catchPhrase":"Multi-layered client-server neural-net",
"bs":"harness real-time e-markets"
}
},
{
"id":2,
"name":"Ervin Howell",
"username":"Antonette",
"email":"Shanna@melissa.tv",
"address":{
"street":"Victor Plains",
"suite":"Suite 879",
"city":"Wisokyburgh",
"zipcode":"90566-7771",
"geo":{
"lat":"-43.9509",
"lng":"-34.4618"
}
},
"phone":"010-692-6593 x09125",
"website":"anastasia.net",
"company":{}
}
]

我的数据模型class如下

    data class MyModel (
    val id: Long,
    val name: String,
    val username: String,
    val email: String,
    val address: Address,
    val phone: String,
    val website: String,
    val company: Company
){

}
data class Address (
    val street: String,
    val suite: String,
    val city: String,
    val zipcode: String,
    val geo: Geo
)
data class Geo (
    val lat: String,
    val lng: String
)

data class Company (
    val name: String,
    val catchPhrase: String,
    val bs: String
)

我不知道我的数据模型 class 是否正确以及如何从我的数据模型或 Json[=26 生成 实体 class =] 回应

您可以安装 JSON TO Kotlin class 插件并使用它。为此:

  1. 在 Android Studio 中选择文件 -> 设置

  2. 搜索插件

  3. Select 市场并搜索“JSON TO Kotlin class”。

安装插件后重新启动 Android studio。现在您可以使用此插件将 JSON 转换为 Kotlin class。为此,请按 Alt + k 或按照以下步骤操作:

1- 转到代码

2-select生成

3- select Kotlin 数据 class 来自 json

现在粘贴 JSON 并在提供 class 名称后单击生成按钮。对于您的 JSON 结果是:

data class MyModelItem(
    val address: Address,
    val company: Company,
    val email: String,
    val id: Int,
    val name: String,
    val phone: String,
    val username: String,
    val website: String
)

data class Address(
    val city: String,
    val geo: Geo,
    val street: String,
    val suite: String,
    val zipcode: String
)

class Company(
)

data class Geo(
    val lat: String,
    val lng: String
)

现在您可以创建如下所示的实体:

@Entity
data class MyModelEntity(
 @PrimaryKey(autoGenerate = true)
 val id: Int,
 val city: String,
 val lat: String,
 val lng: String,
 val street: String,
 val suite: String,
 val zipcode: String,
 val company: Company,
 val email: String,
 val name: String,
 val phone: String,
 val username: String,
 val website: String
) 

在您的 dao 中,您可以按以下方式插入数据:

@Insert(onConflict = OnConflictStrategy.IGNORE)
fun addToDatabase(model: MyModelEntity?): Long

终于在您的存储库中可以使用以下代码:

 fun insertToDatabase(myModelItem: MyModelItem) {
    val myModelEntity = MyModelEntity(
        city = myModelItem.address.city,
        lat = myModelItem.address.lat,
        lng = myModelItem.address.lng,
        street = myModelItem.address.street,
        suite = myModelItem.address.suite,
        zipcode = myModelItem.address.zipcode,
        company = myModelItem.company,
        email = myModelItem.email,
        name = myModelItem.name,
        phone = myModelItem.phone,
        username = myModelItem.username,
        website = myModelItem.website
    )
    dao.addToDatabase(myModelEntity)
}

在你的情况下,你可能只有一个 @Entity -> User 和其他你可以用作 @Embeded 对象。

@Entity
data class User (
    @PrimaryKey(autoGenerate = true)
    var id: Long = 0,

    val name: String,
    val username: String,
    val email: String,
    @Embedded val address: Address,
    val phone: String,
    val website: String,
    @Embedded val company: Company
)

完整的源代码在这里https://github.com/dautovicharis/sos_android/tree/q_68390737

在此处阅读有关 Room DB 的更多信息https://developer.android.com/training/data-storage/room