0

I am new for the room database. I have facing a problem to create Entity class from below JsonArray.

Can you help to create Entity class from the below file:

[
{
"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":{}
}
]

and My data model class as below

    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
)

I have no idea that my data model class is correct or not and how to generate Entity class from my data model or Json response

Ketan Patel
  • 2,155
  • 19
  • 27

2 Answers2

1

You can install JSON TO Kotlin class plugin and use it. for doing this:

  1. In Android Studio choose File -> Settings

  2. Search for Plugins

  3. Select Marketplace and search "JSON TO Kotlin class".

enter image description here

After installing the plugin restart Android studio. Now you can use this plugin for converting JSON to Kotlin class. for doing this press Alt + k or follow bellow steps:

1- go to code

2- select generate

3- select Kotlin data classes from json

now paste the JSON and after providing a class name click generate button. for your JSON the result is:

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
)

Now you can create an entity like bellow:

@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
) 

In your dao you can insert data as bellow:

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

Finally in your repositoy you can use bellow codes:

 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)
}
Alireza Barakati
  • 1,016
  • 2
  • 9
  • 23
1

In your case, you can probably have only one @Entity -> User and others you can use as @Embeded objects.

@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
)

enter image description here

The full source code is here: https://github.com/dautovicharis/sos_android/tree/q_68390737

Read more about Room DB here: https://developer.android.com/training/data-storage/room

Haris
  • 4,130
  • 3
  • 30
  • 47
  • Cay you provide a json/entity sample of foreign key used and type converter used. – Ketan Patel Jul 19 '21 at 08:58
  • 1
    @KetanPatel Sure, I'll push new changes with this case -> I'll let you know. – Haris Jul 19 '21 at 09:28
  • @KetanPatel I have pushed new changes with a custom converter for the Address model class. If you don't have any specific reason it's better to stay with Embeded. For more complex cases please check the official Google example -> https://github.com/android/sunflower – Haris Jul 19 '21 at 15:05