nakko
nakko

Reputation: 1

How to use Retrofit2 to deserialize when JsonArray nested to another JsonArray

Here is my Json data.

[
{
    "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
    }
}

]

I use Retrofit2 and can get the correct response when i don't put the lesson_dateand beacon_lesson in my Model.That's means using Retrofit2 ,i can only get the JsonObject in the specific jsonArray. But when there's another JsonArray lesson_date and beacon_lesson is nested in the JsonArray. But if i use the same method in my Model ,it will have no response, even don't have the error message.

Here is my Model class.

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

If i comment the lesson_date &beacon_lesson in the Model, then it can work correctly.And i can get the information. It is very strange.Here is the LessonDate Model.

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

}

Here is the BeaconUser Model.

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

}

Thanks for your time reading my question.

Upvotes: 0

Views: 145

Answers (1)

Emanuel
Emanuel

Reputation: 8106

Even if i cant find your nested JsonArray since it's one Json-Array wrapped around multiple Json-Object's you can get the Value using

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

If you have nested JsonArrays then you should modify your Model like:

Class MyModel {
    List<MyOtherModel> data;
}

while the nested JSONArray is the List within the Model.

In your case it means that LessonDate is another JsonObject (Object->Object) which is not an array and it should work. Please post the Exception to figure out if there is another issue.

Upvotes: 1

Related Questions