I have project with android app that fetch post from wordpress rest api some field that i take is like this list.
[
{
"id": 43600,
"date": "2020-09-07T19:52:47",
"title": {
"rendered": "Video: .... "
},
"content": {
"rendered": "<div class=\"wp-block-embed__wrapper\"></div>",
"protected": false
},
"author": 31,
"featured_media": 43601,
"categories": [
788,
2760
]
}
]
Already read :
https://developer.android.com/training/data-storage/room
How to parse data of WordPress REST API using Retrofit and GSON?
Wordpress API - How to loop through a JSON Array of objects in Android/Java
Android Kotlin parsing nested JSON
Parse Json to Primative Array Kotlin
the closest answer but mostly in converter
Android Room Database: How to handle Arraylist in an Entity?
https://medium.com/@gilesjeremydev/room-through-a-complete-example-ce5c9ed417ba
I tried to save it into local storage with room in single entity. But based on google doc, it would seperate into 2 entities tied with annotation @relation
@Entity(tableName = "post")
data class SomePost(
@PrimaryKey
@field:SerializedName("id")
val id: Int,
@field:SerializedName("date")
val date: String,
@Embedded
@field:SerializedName("title")
val title: PostTitle,
@Embedded
@field:SerializedName("content")
val content: PostContent,
@field:SerializedName("featured_media")
val imageId: Int,
@field:SerializedName("author")
val author: Int
)
@Entity
data class PostCategories(
@PrimaryKey(autoGenerate = true)
val id: Int,
@field:SerializedName("categories")
val postCategories: Int
)
data class SomePostRelationship (
@Embedded
var post: SomePost? = null,
@Relation(
parentColumn = "id",
entityColumn = "categories"
)
var categories: List<PostCategories>? = null
)
interface PostService {
companion object {
const val ENDPOINT = "https://example.com/wp-json/"
}
// Posts
@GET("wp/v2/posts/")
suspend fun getPostAll(
@Query("page") page: Int? = null,
@Query("per_page") perPage: Int? = null,
@Query("search") search: String? = null,
@Query("order") order: String? = null
): Response<List<somePost>>
The problem is data class PostCategories.
My question is how to serialization json array into entity data class of categories for android room.
if there already answer or same question hope can link to it.