0

I'm getting below response from GitHub API

{
    "url": "https://api.github.com/repos/square/okhttp/issues/6235",
    "repository_url": "https://api.github.com/repos/square/okhttp",
    "labels_url": "https://api.github.com/repos/square/okhttp/issues/6235/labels{/name}",
    "comments_url": "https://api.github.com/repos/square/okhttp/issues/6235/comments",
    "events_url": "https://api.github.com/repos/square/okhttp/issues/6235/events",
    "html_url": "https://github.com/square/okhttp/pull/6235",
    "id": 684864978,
    "node_id": "MDExOlB1bGxSZXF1ZXN0NDcyNjgxNjU4",
    "number": 6235,
    "title": "Document interceptor throwing modes",
    "user": {
        "login": "swankjesse",
        "id": 133019,
        "node_id": "MDQ6VXNlcjEzMzAxOQ==",
        "avatar_url": "https://avatars3.githubusercontent.com/u/133019?v=4",
        "gravatar_id": "",
        "url": "https://api.github.com/users/swankjesse",
        "html_url": "https://github.com/swankjesse",
        "followers_url": "https://api.github.com/users/swankjesse/followers",
        "following_url": "https://api.github.com/users/swankjesse/following{/other_user}",
        "gists_url": "https://api.github.com/users/swankjesse/gists{/gist_id}",
        "starred_url": "https://api.github.com/users/swankjesse/starred{/owner}{/repo}",
        "subscriptions_url": "https://api.github.com/users/swankjesse/subscriptions",
        "organizations_url": "https://api.github.com/users/swankjesse/orgs",
        "repos_url": "https://api.github.com/users/swankjesse/repos",
        "events_url": "https://api.github.com/users/swankjesse/events{/privacy}",
        "received_events_url": "https://api.github.com/users/swankjesse/received_events",
        "type": "User",
        "site_admin": false
    },
    "labels": [

    ],
    "state": "open",
    "locked": false,
    "assignee": null,
    "assignees": [

    ],
    "milestone": null,
    "comments": 0,
    "created_at": "2020-08-24T18:09:47Z",
    "updated_at": "2020-08-24T18:21:18Z",
    "closed_at": null,
    "author_association": "MEMBER",
    "active_lock_reason": null,
    "pull_request": {
        "url": "https://api.github.com/repos/square/okhttp/pulls/6235",
        "html_url": "https://github.com/square/okhttp/pull/6235",
        "diff_url": "https://github.com/square/okhttp/pull/6235.diff",
        "patch_url": "https://github.com/square/okhttp/pull/6235.patch"
    },
    "body": "",
    "performed_via_github_app": null
}

I have tried below code

@Entity(tableName = "issue_list_table")
class IssueModal {

    @SerializedName("id")
    @Expose
    @PrimaryKey
    var id: Int = 0

    @SerializedName("body")
    @Expose
    @ColumnInfo(name = "body")
    var body: String = ""

    @SerializedName("url")
    @Expose
    @ColumnInfo(name = "url")
    var url: String = ""

    @SerializedName("repository_url")
    @Expose
    @ColumnInfo(name = "repository_url")
    var repositoryUrl: String = ""

    @SerializedName("labels_url")
    @Expose
    @ColumnInfo(name = "labels_url")
    var labelsUrl: String = ""

    @SerializedName("comments_url")
    @Expose
    @ColumnInfo(name = "comments_url")
    var commentsUrl: String = ""

    @SerializedName("events_url")
    @Expose
    @ColumnInfo(name = "events_url")
    var eventsUrl: String = ""

    @SerializedName("html_url")
    @Expose
    @ColumnInfo(name = "html_url")
    var htmlUrl: String = ""

    @SerializedName("node_id")
    @Expose
    @ColumnInfo(name = "node_id")
    var nodeId: String = ""

    @SerializedName("number")
    @Expose
    @ColumnInfo(name = "number")
    var number: Int = 0

    @SerializedName("title")
    @Expose
    @ColumnInfo(name = "title")
    var title: String = ""

    @SerializedName("user")
    @Expose
    @Embedded
    var user: User = User()

    @SerializedName("state")
    @Expose
    @ColumnInfo(name = "state")
    var state: String = ""

    @SerializedName("locked")
    @Expose
    @ColumnInfo(name = "locked")
    var locked: Boolean = false

    @SerializedName("comments")
    @Expose
    @ColumnInfo(name = "comments")
    var comments: Int = 0

    @SerializedName("created_at")
    @Expose
    @ColumnInfo(name = "createdAt")
    var createdAt: String = ""

    @SerializedName("updated_at")
    @Expose
    @ColumnInfo(name = "updated_at")
    var updatedAt: String = ""

    @SerializedName("author_association")
    @Expose
    @ColumnInfo(name = "author_association")
    var authorAssociation: String = ""


}

User Class

class User {

    @SerializedName("id")
    @Expose
    @PrimaryKey
    @ColumnInfo(name = "user_id")
    var id: Int = 0

    @SerializedName("login")
    @Expose
    @ColumnInfo(name = "login")
    var login: String = ""


    @SerializedName("node_id")
    @Expose
    @ColumnInfo(name = "user_node_id")
    var nodeId: String = ""

    @SerializedName("avatar_url")
    @Expose
    @ColumnInfo(name = "avatar_url")
    var avatarUrl: String = ""

    @SerializedName("gravatar_id")
    @Expose
    @ColumnInfo(name = "gravatar_id")
    var gravatarId: String = ""


    @SerializedName("html_url")
    @Expose
    @ColumnInfo(name = "user_html_url")
    var htmlUrl: String = ""

    @SerializedName("followers_url")
    @Expose
    @ColumnInfo(name = "followers_url")
    var followersUrl: String = ""

    @SerializedName("following_url")
    @Expose
    @ColumnInfo(name = "following_url")
    var followingUrl: String = ""

    @SerializedName("gists_url")
    @Expose
    @ColumnInfo(name = "gists_url")
    var gistsUrl: String = ""

    @SerializedName("starred_url")
    @Expose
    @ColumnInfo(name = "starred_url")
    var starredUrl: String = ""

    @SerializedName("subscriptions_url")
    @Expose
    @ColumnInfo(name = "subscriptions_url")
    var subscriptionsUrl: String = ""

    @SerializedName("organizations_url")
    @Expose
    @ColumnInfo(name = "organizations_url")
    var organizationsUrl: String = ""

    @SerializedName("repos_url")
    @Expose
    @ColumnInfo(name = "repos_url")
    var reposUrl: String = ""

    @SerializedName("events_url")
    @Expose
    @ColumnInfo(name = "user_events_url")
    var eventsUrl: String = ""

    @SerializedName("received_events_url")
    @Expose
    @ColumnInfo(name = "received_events_url")
    var receivedEventsUrl: String = ""

    @SerializedName("type")
    @Expose
    @ColumnInfo(name = "type")
    var type: String = ""

    @SerializedName("site_admin")
    @Expose
    @ColumnInfo(name = "site_admin")
    var siteAdmin: Boolean = false
}

I want save GitHub api Response in room database but i have no idea how can i create entity class for nested object

If need more information please do let me know. Thanks in advance. Your efforts will be appreciated.

Goku
  • 9,102
  • 8
  • 50
  • 81

2 Answers2

0

You could check this Medium article and see if that helps. It isn't your exact use case, but it does deal with a list of nested objects.

undermark5
  • 775
  • 6
  • 17
0

In your case, you can probably have only one @Entity -> (Your Main Object) and others you can use as @Embeded objects. for example for the User object you use the @Embeded annotation