android - 如何使用 Retrofit Kotlin 从 JSON 对象中的特定字段获取数据
问题描述
我目前正在尝试从 JSON 对象中的特定字段获取数据。API 是 Github API,我尝试实现的功能是搜索。API 结果如下所示
{
"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
}
]
}
我只想从"items"
字段中获取数据。我如何使用 Kotlin 语言中的 Retrofit 来做到这一点?
我尝试使用的代码是这样的:
@GET("search/users")
suspend fun searchUsers(@Query("q") q: String): Response<List<User>>
它给了我错误Expected BEGIN_ARRAY 但是 BEGIN_OBJECT 在第 1 行第 2 列路径 $
更新:这是我的数据类
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
)
任何帮助表示赞赏
解决方案
您还需要将“项目”匹配为数据类:
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>
推荐阅读
- azure - 图表用户中的用户主体名称与邮件
- javascript - JavaScript - 接受字符串和索引(数字)并返回该索引处的字符的函数
- javascript - HTML 选秀/跳棋游戏
- php - 处理奇怪格式的csv文件
- javascript - Javascript后台事件处理循环
- c++ - Visual Studio 2019 窗口中的控制台问题
- mysql - 如何在一对多关系的连接子句中构造数据
- javascript - 脚本继续前的确认模式
- powerbi - Azure 数据工厂如何访问自定义数据连接器
- flowground - Flowground REST-API:流属性状态和 current_status 之间的区别