Reputation: 1501
i am currently trying to get data from specific field in JSON Object. The API is Github API and the feature i try to implement is search. The API results is looks like this
{
"total_count": 70,
"incomplete_results": false,
"items": [
{
"login": "wirya",
"id": 2296318,
"node_id": "MDQ6VXNlcjIyOTYzMTg=",
"avatar_url": "https://avatars2.githubusercontent.com/u/2296318?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/wirya",
"html_url": "https://github.com/wirya",
"followers_url": "https://api.github.com/users/wirya/followers",
"following_url": "https://api.github.com/users/wirya/following{/other_user}",
"gists_url": "https://api.github.com/users/wirya/gists{/gist_id}",
"starred_url": "https://api.github.com/users/wirya/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/wirya/subscriptions",
"organizations_url": "https://api.github.com/users/wirya/orgs",
"repos_url": "https://api.github.com/users/wirya/repos",
"events_url": "https://api.github.com/users/wirya/events{/privacy}",
"received_events_url": "https://api.github.com/users/wirya/received_events",
"type": "User",
"site_admin": false,
"score": 1.0
},
{
"login": "wiryawan46",
"id": 16128117,
"node_id": "MDQ6VXNlcjE2MTI4MTE3",
"avatar_url": "https://avatars0.githubusercontent.com/u/16128117?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/wiryawan46",
"html_url": "https://github.com/wiryawan46",
"followers_url": "https://api.github.com/users/wiryawan46/followers",
"following_url": "https://api.github.com/users/wiryawan46/following{/other_user}",
"gists_url": "https://api.github.com/users/wiryawan46/gists{/gist_id}",
"starred_url": "https://api.github.com/users/wiryawan46/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/wiryawan46/subscriptions",
"organizations_url": "https://api.github.com/users/wiryawan46/orgs",
"repos_url": "https://api.github.com/users/wiryawan46/repos",
"events_url": "https://api.github.com/users/wiryawan46/events{/privacy}",
"received_events_url": "https://api.github.com/users/wiryawan46/received_events",
"type": "User",
"site_admin": false,
"score": 1.0
}
]
}
I want to get data from "items"
field only. How can i do that using Retrofit in Kotlin language?
The code that i tried to use is like this:
@GET("search/users")
suspend fun searchUsers(@Query("q") q: String): Response<List<User>>
It gives me error Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
Update: Here is my data class
data class User(
var login: String,
var type: String,
var avatar_url: String,
var public_repos: Int = 0,
var followers: Int = 0,
var following: Int = 0
)
Any help is appreciated
Upvotes: 0
Views: 1169
Reputation: 824
You need to match 'items' as a data class as well:
data class Items (
val items: List<User>
)
data class User(
var login: String,
var type: String,
var avatar_url: String,
var public_repos: Int = 0,
var followers: Int = 0,
var following: Int = 0
)
@GET("search/users")
suspend fun searchUsers(@Query("q") q: String): Response<Items>
Upvotes: 1