首页 > 解决方案 > 如何使用 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

)

任何帮助表示赞赏

标签: androidkotlinretrofit

解决方案


您还需要将“项目”匹配为数据类:

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>

推荐阅读