如何在改造中对多个 json 数组使用回调?
How to use callback for multiple json array in retrofit?
我正在尝试改造如何从下面的响应中获取数据
{
"schedule_students": [
{
"id": "753",
"sch_id": "153"
},
{
"id": "765",
"sch_id": "153"
}
],
"s_students": [
{
"id": "753",
"s_id": "153"
},
{
"id": "765",
"s_id": "153"
}
],
"schedu": [
{
"id": "753",
"ch_id": "153"
},
{
"id": "765",
"ch_id": "153"
}
],
"delids": "no",
"expdelids": "no",
"lastsyncdate": "2015-06-01 10:33:19"
}
在我的 API 响应中它有多个 JSON 数组。如何从此响应中检索所有数据
创建 POJO (Plain Old Java O bjects) 用于 JSON 数据集。
使用此 tool 生成 POJO。
SStudent.class
package com.example.someapp;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
package com.example.someapp;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class SStudent {
@Expose
private String id;
@SerializedName("s_id")
@Expose
private String sId;
/**
*
* @return
* The id
*/
public String getId() {
return id;
}
/**
*
* @param id
* The id
*/
public void setId(String id) {
this.id = id;
}
/**
*
* @return
* The sId
*/
public String getSId() {
return sId;
}
/**
*
* @param sId
* The s_id
*/
public void setSId(String sId) {
this.sId = sId;
}
}
同理,Other为other生成类。
最后,
SomeClass.java
package com.example.someapp;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class SomeClass {
@SerializedName("schedule_students")
@Expose
private List<ScheduleStudent> scheduleStudents = new ArrayList<ScheduleStudent>();
@SerializedName("s_students")
@Expose
private List<SStudent> sStudents = new ArrayList<SStudent>();
@Expose
private List<Schedu> schedu = new ArrayList<Schedu>();
@Expose
private String delids;
@Expose
private String expdelids;
@Expose
private String lastsyncdate;
/**
*
* @return
* The scheduleStudents
*/
public List<ScheduleStudent> getScheduleStudents() {
return scheduleStudents;
}
/**
*
* @param scheduleStudents
* The schedule_students
*/
public void setScheduleStudents(List<ScheduleStudent> scheduleStudents) {
this.scheduleStudents = scheduleStudents;
}
/**
*
* @return
* The sStudents
*/
public List<SStudent> getSStudents() {
return sStudents;
}
/**
*
* @param sStudents
* The s_students
*/
public void setSStudents(List<SStudent> sStudents) {
this.sStudents = sStudents;
}
/**
*
* @return
* The schedu
*/
public List<Schedu> getSchedu() {
return schedu;
}
/**
*
* @param schedu
* The schedu
*/
public void setSchedu(List<Schedu> schedu) {
this.schedu = schedu;
}
/**
*
* @return
* The delids
*/
public String getDelids() {
return delids;
}
/**
*
* @param delids
* The delids
*/
public void setDelids(String delids) {
this.delids = delids;
}
/**
*
* @return
* The expdelids
*/
public String getExpdelids() {
return expdelids;
}
/**
*
* @param expdelids
* The expdelids
*/
public void setExpdelids(String expdelids) {
this.expdelids = expdelids;
}
/**
*
* @return
* The lastsyncdate
*/
public String getLastsyncdate() {
return lastsyncdate;
}
/**
*
* @param lastsyncdate
* The lastsyncdate
*/
public void setLastsyncdate(String lastsyncdate) {
this.lastsyncdate = lastsyncdate;
}
}
像这样在 Retrofit API 界面中使用 SomeClass.java。
@GET("/your_api_endpoint")
SomeClass getSomeClass(@Query("param") int param);
然后在调用getSomeClass(param)
后作为SomeClass的对象正常访问。
我正在尝试改造如何从下面的响应中获取数据
{
"schedule_students": [
{
"id": "753",
"sch_id": "153"
},
{
"id": "765",
"sch_id": "153"
}
],
"s_students": [
{
"id": "753",
"s_id": "153"
},
{
"id": "765",
"s_id": "153"
}
],
"schedu": [
{
"id": "753",
"ch_id": "153"
},
{
"id": "765",
"ch_id": "153"
}
],
"delids": "no",
"expdelids": "no",
"lastsyncdate": "2015-06-01 10:33:19"
}
在我的 API 响应中它有多个 JSON 数组。如何从此响应中检索所有数据
创建 POJO (Plain Old Java O bjects) 用于 JSON 数据集。 使用此 tool 生成 POJO。
SStudent.class
package com.example.someapp;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
package com.example.someapp;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class SStudent {
@Expose
private String id;
@SerializedName("s_id")
@Expose
private String sId;
/**
*
* @return
* The id
*/
public String getId() {
return id;
}
/**
*
* @param id
* The id
*/
public void setId(String id) {
this.id = id;
}
/**
*
* @return
* The sId
*/
public String getSId() {
return sId;
}
/**
*
* @param sId
* The s_id
*/
public void setSId(String sId) {
this.sId = sId;
}
}
同理,Other为other生成类。
最后,
SomeClass.java
package com.example.someapp;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class SomeClass {
@SerializedName("schedule_students")
@Expose
private List<ScheduleStudent> scheduleStudents = new ArrayList<ScheduleStudent>();
@SerializedName("s_students")
@Expose
private List<SStudent> sStudents = new ArrayList<SStudent>();
@Expose
private List<Schedu> schedu = new ArrayList<Schedu>();
@Expose
private String delids;
@Expose
private String expdelids;
@Expose
private String lastsyncdate;
/**
*
* @return
* The scheduleStudents
*/
public List<ScheduleStudent> getScheduleStudents() {
return scheduleStudents;
}
/**
*
* @param scheduleStudents
* The schedule_students
*/
public void setScheduleStudents(List<ScheduleStudent> scheduleStudents) {
this.scheduleStudents = scheduleStudents;
}
/**
*
* @return
* The sStudents
*/
public List<SStudent> getSStudents() {
return sStudents;
}
/**
*
* @param sStudents
* The s_students
*/
public void setSStudents(List<SStudent> sStudents) {
this.sStudents = sStudents;
}
/**
*
* @return
* The schedu
*/
public List<Schedu> getSchedu() {
return schedu;
}
/**
*
* @param schedu
* The schedu
*/
public void setSchedu(List<Schedu> schedu) {
this.schedu = schedu;
}
/**
*
* @return
* The delids
*/
public String getDelids() {
return delids;
}
/**
*
* @param delids
* The delids
*/
public void setDelids(String delids) {
this.delids = delids;
}
/**
*
* @return
* The expdelids
*/
public String getExpdelids() {
return expdelids;
}
/**
*
* @param expdelids
* The expdelids
*/
public void setExpdelids(String expdelids) {
this.expdelids = expdelids;
}
/**
*
* @return
* The lastsyncdate
*/
public String getLastsyncdate() {
return lastsyncdate;
}
/**
*
* @param lastsyncdate
* The lastsyncdate
*/
public void setLastsyncdate(String lastsyncdate) {
this.lastsyncdate = lastsyncdate;
}
}
像这样在 Retrofit API 界面中使用 SomeClass.java。
@GET("/your_api_endpoint")
SomeClass getSomeClass(@Query("param") int param);
然后在调用getSomeClass(param)
后作为SomeClass的对象正常访问。