Storing a nested JSON object into Android Persistence Library

血红的双手。 提交于 2020-06-27 01:08:22

问题


from my local Django Rest Framework service I get the following JSON output:

{
    "count": 5,
    "next": null,
    "previous": null,
    "results": [
        {
            "id": 1,
            "created": "2020-04-18T16:00:16.060915Z",
            "name": "Germany",
            "groups": [
                {
                    "id": 1,
                    "created": "2020-04-18T16:03:11.138661Z",
                    "name": "MyGroup1",
                    "owner_id": 1
                },
                {
                    "id": 2,
                    "created": "2020-04-18T16:03:20.701660Z",
                    "name": "MyGroup2",
                    "owner_id": 1
                }, 
                ... 

Each Country can have many Groups. For this I have created the following data classes in my Android App project:

@JsonClass(generateAdapter = true)
data class NetworkCountryContainer(
    val count: Long,
    val next: String?,
    val previous: String?,
    val results: List<Country>
)

@Entity(tableName = "country_table")
@JsonClass(generateAdapter = true)
data class Country(
    @PrimaryKey
    @Json(name="id")
    val countryId : Int,
    @Json(name="name")
    val countryName: String,
    @Json(name="groups")
    val groupList: List<Group>       // <--- this field causes the ERROR
)

@Entity(tableName = "group_table")
@JsonClass(generateAdapter = true)
data class Group(
    @PrimaryKey
    @Json(name="id")
    val groupId : Int,
    @Json(name="name")
    val groupName: String,
    @Json(name="owner_id")
    val ownerId: Int
)

Android Studio tells me this:

Cannot figure out how to save this field into database. You can consider adding a type converter for it.

Why I need a TypeConverter ? And how can I build one ?

来源:https://stackoverflow.com/questions/61304472/storing-a-nested-json-object-into-android-persistence-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!