【发布时间】: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<Specialty> specialties应该是Employee的属性,而不是ResponseModel。这可能是问题吗? -
@Leon 我调试应用程序并在响应时获得完整正确的员工列表,但它不包含具有专业知识的数组。正确:具有特长的数组为空,未填充
-
Employee 类是否有一个应该存储此数据的专业属性?为什么
ResponseModel有一个specialties属性,而您提供的示例JSON 不包含这样一个专业列表,但每个员工都有一个? -
请添加您的员工模型类
-
查看调试器输出,
Specialty类属性名称似乎不正确,分别是specId和specName而不是specialty_id和name。也许还可以将Specialty类添加到问题中。
标签: android json retrofit gson