当JsonArray嵌套到另一个JsonArray时,如何使用Retrofit2反序列化

时间:2017-09-08 07:33:23

标签: java android json retrofit2 gson

这是我的Json数据。

[
{
    "id": 280866,
    "student_id": 7,
    "lesson_date_id": 31476,
    "recorded_time": "11:49:55",
    "lecturer_id": null,
    "status": 12895,
    "created_at": "2017-07-31 11:49:55",
    "updated_at": null,
    "lesson_date": {
        "id": 31476,
        "lesson_id": 28,
        "ldate": "2017-07-31",
        "updated_by": 1,
        "created_at": "2017-04-19 03:33:43",
        "updated_at": null
    },
    "lecturer": null,
    "lesson": {
        "id": 28,
        "semester": "2",
        "module_id": "009521",
        "subject_area": "IS PDA",
        "catalog_number": "7COMISS",
        "class_section": "T03",
        "component": "TUT",
        "facility": "05-04-0009",
        "venue_id": 2,
        "weekday": "2",
        "start_time": "08:00:00",
        "end_time": "12:00:00",
        "meeting_pattern": "",
        "created_at": "0000-00-00 00:00:00",
        "updated_at": "2017-07-10 10:19:51",
        "lesson_name": null,
        "credit_unit": null
    }
},
{
    "id": 284077,
    "student_id": 6,
    "lesson_date_id": 31476,
    "recorded_time": "00:00:24",
    "lecturer_id": null,
    "status": -1,
    "created_at": "2017-08-01 00:00:23",
    "updated_at": null,
    "lesson_date": {
        "id": 31476,
        "lesson_id": 28,
        "ldate": "2017-07-31",
        "updated_by": 1,
        "created_at": "2017-04-19 03:33:43",
        "updated_at": null
    },
    "lecturer": null,
    "lesson": {
        "id": 28,
        "semester": "2",
        "module_id": "009521",
        "subject_area": "IS PDA",
        "catalog_number": "7COMISS",
        "class_section": "T03",
        "component": "TUT",
        "facility": "05-04-0009",
        "venue_id": 2,
        "weekday": "2",
        "start_time": "08:00:00",
        "end_time": "12:00:00",
        "meeting_pattern": "",
        "created_at": "0000-00-00 00:00:00",
        "updated_at": "2017-07-10 10:19:51",
        "lesson_name": null,
        "credit_unit": null
    }
},
{
    "id": 284076,
    "student_id": 5,
    "lesson_date_id": 31476,
    "recorded_time": "00:00:24",
    "lecturer_id": null,
    "status": -1,
    "created_at": "2017-08-01 00:00:23",
    "updated_at": null,
    "lesson_date": {
        "id": 31476,
        "lesson_id": 28,
        "ldate": "2017-07-31",
        "updated_by": 1,
        "created_at": "2017-04-19 03:33:43",
        "updated_at": null
    },
    "lecturer": null,
    "lesson": {
        "id": 28,
        "semester": "2",
        "module_id": "009521",
        "subject_area": "IS PDA",
        "catalog_number": "7COMISS",
        "class_section": "T03",
        "component": "TUT",
        "facility": "05-04-0009",
        "venue_id": 2,
        "weekday": "2",
        "start_time": "08:00:00",
        "end_time": "12:00:00",
        "meeting_pattern": "",
        "created_at": "0000-00-00 00:00:00",
        "updated_at": "2017-07-10 10:19:51",
        "lesson_name": null,
        "credit_unit": null
    }
},
{
    "id": 284075,
    "student_id": 4,
    "lesson_date_id": 31476,
    "recorded_time": "00:00:24",
    "lecturer_id": null,
    "status": -1,
    "created_at": "2017-08-01 00:00:23",
    "updated_at": null,
    "lesson_date": {
        "id": 31476,
        "lesson_id": 28,
        "ldate": "2017-07-31",
        "updated_by": 1,
        "created_at": "2017-04-19 03:33:43",
        "updated_at": null
    },
    "lecturer": null,
    "lesson": {
        "id": 28,
        "semester": "2",
        "module_id": "009521",
        "subject_area": "IS PDA",
        "catalog_number": "7COMISS",
        "class_section": "T03",
        "component": "TUT",
        "facility": "05-04-0009",
        "venue_id": 2,
        "weekday": "2",
        "start_time": "08:00:00",
        "end_time": "12:00:00",
        "meeting_pattern": "",
        "created_at": "0000-00-00 00:00:00",
        "updated_at": "2017-07-10 10:19:51",
        "lesson_name": null,
        "credit_unit": null
    }
},
{
    "id": 280865,
    "student_id": 2,
    "lesson_date_id": 31476,
    "recorded_time": "11:49:55",
    "lecturer_id": null,
    "status": 12895,
    "created_at": "2017-07-31 11:49:55",
    "updated_at": null,
    "lesson_date": {
        "id": 31476,
        "lesson_id": 28,
        "ldate": "2017-07-31",
        "updated_by": 1,
        "created_at": "2017-04-19 03:33:43",
        "updated_at": null
    },
    "lecturer": null,
    "lesson": {
        "id": 28,
        "semester": "2",
        "module_id": "009521",
        "subject_area": "IS PDA",
        "catalog_number": "7COMISS",
        "class_section": "T03",
        "component": "TUT",
        "facility": "05-04-0009",
        "venue_id": 2,
        "weekday": "2",
        "start_time": "08:00:00",
        "end_time": "12:00:00",
        "meeting_pattern": "",
        "created_at": "0000-00-00 00:00:00",
        "updated_at": "2017-07-10 10:19:51",
        "lesson_name": null,
        "credit_unit": null
    }
}

我使用Retrofit2并且在我没有将lesson_datebeacon_lesson放入我的模型中时可以得到正确的答案。这意味着使用Retrofit2,我只能得到JsonObject在特定的jsonArray中。但是当有另一个JsonArray lesson_datebeacon_lesson嵌套在JsonArray中时。但是如果我在我的模型中使用相同的方法,它将没有响应,甚至没有错误信息。

这是我的Model类。

public class TimetableResult {
    @SerializedName("id")
    @Expose
    private String id;

    @SerializedName("lesson_id")
    @Expose
    private String lesson_id;

    @SerializedName("lecturer_id")
    @Expose
    private String lecturer_id;

    @SerializedName("lesson")
    @Expose
    private Lesson lesson;

    @SerializedName("venue")
    @Expose
    private Venue venue;

    @SerializedName("lesson_date")
    @Expose
    private LessonDate lesson_date;

    @SerializedName("beaconLesson")
    @Expose
    private LessonBeacon lessonBeacon;


    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getLesson_id() {
        return lesson_id;
    }

    public void setLesson_id(String lesson_id) {
        this.lesson_id = lesson_id;
    }

    public  String getLecturer_id(){return lecturer_id;}

    public void  setLecturer_id(String lecturer_id){this.lecturer_id=lecturer_id;}

    public Lesson getLesson() {
        return lesson;
    }

    public void setLesson(Lesson lesson) {
        this.lesson = lesson;
    }

    public LessonDate getLesson_date() {
        return lesson_date;
    }

    public void setLesson_date(LessonDate lesson_date) {
        this.lesson_date = lesson_date;
    }

    public Venue getVenue() {
        return venue;
    }

    public void setVenue(Venue venue) {
        this.venue = venue;
    }

    public LessonBeacon getLessonBeacon() {
        return lessonBeacon;
    }

    public void setLessonBeacon(LessonBeacon lessonBeacon) {
        this.lessonBeacon = lessonBeacon;
    }

如果我在模型中评论lesson_date& beacon_lesson,那么它可以正常工作。我可以获取信息。这很奇怪。这是LessonDate模型。

public class LessonDate {
@SerializedName("id")
@Expose
private String id;

@SerializedName("lesson_id")
@Expose
private String lesson_id;

@SerializedName("ldate")
@Expose
private String ldate;

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getLesson_id() {
    return lesson_id;
}

public void setLesson_id(String lesson_id) {
    this.lesson_id = lesson_id;
}

public String getLdate() {
    return ldate;
}

public void setLdate(String ldate) {
    this.ldate = ldate;
}

public String getDate()
{
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    Date startDate;
    String week_day = null;
    try
    {
        startDate = df.parse(ldate);
        week_day = getWeekDay(startDate.getDay());
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    return week_day;
}

private String getWeekDay(int day)
{
    String strWeekDay = null;
    switch (day) {
        case 1:
            strWeekDay = "Monday";
            break;
        case 2:
            strWeekDay = "Tuesday";
            break;
        case 3:
            strWeekDay = "Wednesday";
            break;
        case 4:
            strWeekDay = "Thursday";
            break;
        case 5:
            strWeekDay = "Friday";
            break;
        case 6:
            strWeekDay = "Saturday";
            break;
        case 7:
            strWeekDay = "Sunday";
            break;
    }

    return strWeekDay;
}

}

这是BeaconUser模型。

public class BeaconUser {
@SerializedName("id")
@Expose
private String id;

@SerializedName("major")
@Expose
private String major;

@SerializedName("minor")
@Expose
private String minor;

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getMajor() {
    return major;
}

public void setMajor(String major) {
    this.major = major;
}

public String getMinor() {
    return minor;
}

public void setMinor(String minor) {
    this.minor = minor;
}

}

感谢您抽出时间阅读我的问题。

1 个答案:

答案 0 :(得分:1)

即使我找不到你嵌套的JsonArray,因为它有一个Json-Array包裹着多个Json-Object,你可以使用

获得价值
@GET("path/to/your/data")
List<BeaconUser> Users();

如果你有嵌套的JsonArrays,那么你应该修改你的模型,如:

Class MyModel {
    List<MyOtherModel> data;
}

而嵌套的JSONArray是模型中的List。

在你的情况下,它意味着LessonDate是另一个JsonObject(Object-&gt; Object),它不是一个数组,它应该工作。请发布例外以确定是否存在其他问题。