【问题标题】:Get full response from JSON with Retrofit使用 Retrofit 从 JSON 获得完整响应
【发布时间】:2018-08-07 10:40:55
【问题描述】:

我试图用内部专业列表解析 json 我有这种格式的 JSON

{
"response" : [
    {
        "f_name"    : "иВан",
        "l_name"    : "ИваноВ",
        "birthday"  : "1987-03-23",
        "avatr_url" : "http://url/upimg/author/451/image.jpg",
        "specialty" : [{
            "specialty_id" : 101,
            "name"  : "Менеджер"
        }]
    },
    {
        "f_name"    : "Петр",
        "l_name"    : "петроВ",
        "birthday"  : null,
        "avatr_url" : "http://url/upimg/author/489/image.jpg",
        "specialty" : [{
            "specialty_id" : 101,
            "name"  : "Менеджер"
        },
        {
            "specialty_id" : 102,
            "name"  : "Разработчик"
        }]
    },....

我通过这种方法获取数据:

instance = this;
    Room.databaseBuilder(this, AppDatabase.class, "database").build();
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    readerApi = retrofit.create(ReaderApi.class);
}

public static ReaderApi getApi() {
    return readerApi;
}
....
public interface ReaderApi {
@GET("testTask.json")
Call<ResponseModel> getJSON();
}

没关系,我正确获取了所有员工数据。

但我无法获得专业数组。调试中的specialty_id 和specialty_name = null。

也是我的反应模型:

public class ResponseModel {
private ArrayList<Employee> employees = new ArrayList<>();
private ArrayList<Specialty> specialties = new ArrayList<>();
@Ignore
public ResponseModel(ArrayList<Employee> employees, ArrayList<Specialty> specialties) {
    this.employees = employees;
    this.specialties = specialties;
}
some getters...
...
}

我的员工模型:

@Entity(indices = {@Index(value = {"f_name", "l_name", "birthday", 
"avatarLink"})})
public class Employee {
@PrimaryKey(autoGenerate = true)
public int employeeId;
public String f_name;
public String l_name;
public String birthday;
@Ignore
private int age;
public String avatarLink;
@Ignore
private List<Specialty> specialty;
public Employee(int employeeId, String f_name, String l_name, String birthday, String avatarLink) {
    this.employeeId = employeeId;
    this.f_name = f_name;
    this.l_name = l_name;
    this.birthday = birthday;
    this.avatarLink = avatarLink;
}
  • @Ignore for Android Room

【问题讨论】:

  • 查看参考 json 时,看起来 List&lt;Specialty&gt; specialties 应该是 Employee 的属性,而不是 ResponseModel。这可能是问题吗?
  • @Leon 我调试应用程序并在响应时获得完整正确的员工列表,但它不包含具有专业知识的数组。正确:具有特长的数组为空,未填充
  • Employee 类是否有一个应该存储此数据的专业属性?为什么ResponseModel 有一个specialties 属性,而您提供的示例JSON 不包含这样一个专业列表,但每个员工都有一个?
  • 请添加您的员工模型类
  • 查看调试器输出,Specialty 类属性名称似乎不正确,分别是 specIdspecName 而不是 specialty_idname。也许还可以将Specialty 类添加到问题中。

标签: android json retrofit gson


【解决方案1】:

问题是参数 JSON 和类字段的名称不匹配。 正确:

如果 JSON 有这个:

"f_name"    : "Name",
    "l_name"    : "LName",
    "birthday"  : String,
    "avatr_url" : "URL",
    "specialty" : [{
        "specialty_id" : int,
        "name"  : "Name"
    }...

在课堂上我们需要使用这个:

public String f_name;
public String l_name;
public String birthday;
....
public int specialty_id;
public String name;
....
public Specialty(int specialty_id, String name) {
    this.specialty_id = specialty_id;
    this.name = name;
}

感谢@Leon 的回答

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-10
    • 2016-11-18
    • 1970-01-01
    • 2016-11-03
    • 2016-02-25
    • 2018-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多