简体   繁体   English

通过Retrofit获得JSON的完整响应

[英]Get full response from JSON with Retrofit

I trying to parse json with inner list of specialties I have JSON in this format: 我试图用内部特殊列表解析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"  : "Разработчик"
        }]
    },....

I get data by this method: 我通过这种方法获取数据:

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();
}

This is OK, I get all employee data correctly. 可以,我可以正确获取所有员工数据。

But i cant get specialties array. 但我不能得到特色数组。 specialty_id and specialty_name = null in debugging. Specialty_id和Specialty_name =在调试中为null。

Also my response model: 还有我的响应模型:

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...
...
}

调试屏幕截图

My Employee model: 我的员工模型:

@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 @忽略Android Room

Problem was the mismatched names of parameters JSON and class fields. 问题是参数JSON和类字段的名称不匹配。 Correctly: 正确:

if JSON have this: 如果JSON具有以下内容:

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

in class we need to use this: 在课堂上,我们需要使用以下代码:

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;
}

Thank you for @Leon for answer 谢谢@Leon的回答

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM