【问题标题】:Get JSON list embeded another object with Retrofit使用 Retrofit 获取嵌入另一个对象的 JSON 列表
【发布时间】:2017-12-08 23:48:16
【问题描述】:

我有一个提供 JSON 的服务器休息。我可以使用 Retrofit 获取用户,但不能获取用户列表。

用户 JSON 示例(服务器/用户/2):

{
  "id": 2,
  "firstName": "name",
  "lastName": "name"
}

用户列表示例(服务器/用户):

{
  "_embedded": {
    "users": [
      {
        "id": 2,
        "firstName": "name",
        "lastName": "name"
      },
      {
        "id": 3,
        "firstName": "Ime",
        "lastName": "Léonide"
      }
    ]
  }
}

我猜这是由“_embedded”引起的。我认为我无法改变这一点。

Java 代码:

@GET("users/")
Call<List<User>> usersList();

【问题讨论】:

    标签: java json retrofit


    【解决方案1】:

    试试:

    @GET("users/")
    Call<UserResponse> usersList();
    
    class UserResponse {
        @SerializedName("_embedded")
        UserList embedded;
    }
    
    class UserList {
        @SerializedName("users")
        List<User> userList;
    }
    

    【讨论】:

      猜你喜欢
      • 2022-01-21
      • 2014-01-04
      • 1970-01-01
      • 1970-01-01
      • 2021-11-12
      • 2020-09-29
      • 1970-01-01
      • 1970-01-01
      • 2020-04-02
      相关资源
      最近更新 更多