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