Reputation: 956
I need your help. I try to find the solution without deserialize but I didn't find nothing. It would be nice if you can help me... So I have this following JSON file
"resources": [{
"id": 441988,
"name": "TVA",
"parent": {
"id": 159
},
},
{
"id": 441900,
"name": "Marketing",
"parent": {
"id": 166
},
}]
I used ROOM to create a database, it works but I have one problem, its impossible to save "id of Parent object"
here is my following code :
@Entity(tableName = "resources")
class CategoryBean {
@PrimaryKey
@ColumnInfo(name = "resource_id")
@SerializedName("id")
private var mId: Int = 0
@ColumnInfo(name = "name")
@SerializedName("name")
private var mName: String? = null
@ColumnInfo(name = "parent")
@SerializedName("parent")
private var mParent: ParentBean? = null
fun getId(): Int {
return mId
}
fun getName(): String? {
return mName
}
fun getParent(): ParentBean? {
return mParent
}
fun setParent(parent: ParentBean) {
mParent = parent
}
fun setId(id: Int) {
mId = id
}
fun setName(name: String) {
mName = name
}
Here you can find my ParentBean file
class ParentBean {
@SerializedName("id")
private var mId: Int = 0
fun getId(): Int? {
return mId
}
}
I found some solution for a list, but not for sub object.
thank you for your time and your help.
There is my following error :
Cannot figure out how to save this field into database. You can consider adding a type converter for it.
Upvotes: 1
Views: 1855
Reputation: 1126
Room works on top of a SQLite database, when you annotate a class as @Entity
you are creating a table whose columns are defined according to the properties of the class, so you can't store directly a custom type like ParentBean
, to do that, you need to annotate mParent
property as @Embedded
:
@Entity(tableName = "resources")
class CategoryBean constructor(
@PrimaryKey
@ColumnInfo(name = "resource_id")
@SerializedName("id")
var mId: Int,
@ColumnInfo(name = "name")
@SerializedName("name")
var mName: String?,
@SerializedName("parent")
@Embedded
var mParent: ParentBean?
)
Then you define a column name for the property in ParentBean
:
data class ParentBean constructor(@SerializedName("id") @ColumnInfo(name = "parent_id")var mId: Int)
resources table will have the following columns: resource_id
, name
, parent_id
source: https://developer.android.com/training/data-storage/room/defining-data
Note: You don't need to manually define getter and setters in Kotlin.
Upvotes: 5
Reputation: 588
Your problem is your JSON data is not valid.
"resources": [{
"id": 441988,
"name": "TVA",
"parent": {
"id": 159
}, //This Line has a comma without another field following it
},
{
"id": 441900,
"name": "Marketing",
"parent": {
"id": 166
}, //This Line has a comma without another field following it
}]
Also the entire thing should be surrounded by another set of curly braces (but I am assuming that was left out due to copy & paste).
A good tool to test out your JSON data and your POJOs is this site: http://www.jsonschema2pojo.org
Hope this helps :)
Upvotes: 0