The entire JSON gets parsed and gets converted to its corresponding model object using GSON.
For some reasons, if the JSONArray
"video" is empty, the GSON throws an exception.
While parsing the JSONArray "video" if the array is empty-([]) then parsing will throws exception
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 445 path $.video.
If the "video" is an empty JsonArray, what I need is the model object pointing to that jsonarray needs to be null.
Model Object For Video:
public class Video {
@SerializedName("date")
@Expose
private String date;
@SerializedName("video_list")
@Expose
private List<VideoList> videoList = null;
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public List<VideoList> getVideoList() {
return videoList;
}
public void setVideoList(List<VideoList> videoList) {
this.videoList = videoList;
}
public static List<CustomVideoList> getCustomizedVideoList(List<Video> videos) {
List<CustomVideoList> customVideoList = new ArrayList<>();
if (videos != null && videos.size() > 0) {
for (Video video : videos) {
String headerDate = "";
CustomVideoList customVideoHeader = new CustomVideoList();
customVideoHeader.setHeader(true);
headerDate = video.getDate();
customVideoHeader.setHeaderDate(headerDate);
customVideoList.add(customVideoHeader);
for (VideoList videoList : video.getVideoList()) {
CustomVideoList customVideo = new CustomVideoList();
customVideo.setId(videoList.getId());
customVideo.setScvName(videoList.getScvName());
customVideo.setScvDescription(videoList.getScvDescription());
customVideo.setScvComments(videoList.getScvComments());
customVideo.setScvStatus(videoList.getScvStatus());
customVideo.setScvType(videoList.getScvType());
customVideo.setScvUrl(videoList.getScvUrl());
customVideo.setIdPlayer(videoList.getIdPlayer());
customVideo.setScvCreatedby(videoList.getScvCreatedby());
customVideo.setScvCreated(videoList.getScvCreated());
customVideo.setScvModifiedby(videoList.getScvModifiedby());
customVideo.setScvModified(videoList.getScvModified());
customVideo.setScvThumb(videoList.getScvThumb());
customVideo.setHeaderDate(headerDate);
customVideoList.add(customVideo);
}
}
}
return customVideoList;
}
}
The Json what cause exception since "video" is empty JSONArray:
{
"status": 1,
"status_code": 101,
"message": "Success",
"player": [{
"ID": "5",
"user_name": "player2",
"user_email": "player2@gmail.com",
"mem_name": "player2",
"player_pic": "dp3.png",
"mem_address": "player2 address",
"mem_location": "player2 location",
"mem_gender": "1",
"mem_dob": "2018-07-17 00:00:00",
"mem_joined": "2018-07-17 00:00:00",
"mem_createdby": "2",
"mem_created": "2018-07-17 00:00:00",
"mem_modifiedby": "2",
"mem_modified": "2018-07-17 00:00:00"
}],
"video": [],
"image": []
}
The Json that gets parsed succesfully
{
"status": 1,
"status_code": 101,
"message": "Success",
"player": [
{
"ID": "4",
"user_name": "player",
"user_email": "player1@gmail.com",
"mem_name": "player",
"player_pic": "dp2.png",
"mem_address": "player address",
"mem_location": "player location",
"mem_gender": "1",
"mem_dob": "2018-07-17 00:00:00",
"mem_joined": "2018-07-17 00:00:00",
"mem_createdby": "2",
"mem_created": "2018-07-17 00:00:00",
"mem_modifiedby": "2",
"mem_modified": "2018-07-17 00:00:00"
}
],
"video": [
{
"date": "21/07/2018",
"video_list": [
{
"id": "1",
"scv_name": "Practice 1",
"scv_description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.",
"scv_comments": "Lorem Ipsum is simply dummy text",
"scv_status": "1",
"scv_type": "1",
"scv_thumb": "3.jpg",
"scv_url": "3.mp4",
"id_player": "4",
"scv_createdby": "5",
"scv_created": "2018-07-21 00:00:00",
"scv_modifiedby": "5",
"scv_modified": "2018-07-21 00:00:00"
},
{
"id": "3",
"scv_name": "Practice 3",
"scv_description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.",
"scv_comments": "Lorem Ipsum is simply dummy text",
"scv_status": "1",
"scv_type": "1",
"scv_thumb": "2.jpg",
"scv_url": "2.mp4",
"id_player": "4",
"scv_createdby": "5",
"scv_created": "2018-07-21 00:00:00",
"scv_modifiedby": "5",
"scv_modified": "2018-07-21 00:00:00"
},
{
"id": "4",
"scv_name": "Practice 3",
"scv_description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.",
"scv_comments": "Lorem Ipsum is simply dummy text",
"scv_status": "1",
"scv_type": "1",
"scv_thumb": "3.jpg",
"scv_url": "3.mp4",
"id_player": "4",
"scv_createdby": "5",
"scv_created": "2018-07-21 00:00:00",
"scv_modifiedby": "5",
"scv_modified": "2018-07-21 00:00:00"
}
]
},
{
"date": "20/07/2018",
"video_list": [
{
"id": "2",
"scv_name": "Practice 2",
"scv_description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.",
"scv_comments": "Lorem Ipsum is simply dummy text",
"scv_status": "1",
"scv_type": "1",
"scv_thumb": "1.jpg",
"scv_url": "1.mp4",
"id_player": "4",
"scv_createdby": "5",
"scv_created": "2018-07-20 00:00:00",
"scv_modifiedby": "5",
"scv_modified": "2018-07-22 00:00:00"
},
{
"id": "6",
"scv_name": "Practice 5",
"scv_description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.",
"scv_comments": "Lorem Ipsum is simply dummy text",
"scv_status": "1",
"scv_type": "1",
"scv_thumb": "3.jpg",
"scv_url": "3.mp4",
"id_player": "4",
"scv_createdby": "7",
"scv_created": "2018-07-20 00:00:00",
"scv_modifiedby": "7",
"scv_modified": "2018-07-21 00:00:00"
}
]
},
{
"date": "18/07/2018",
"video_list": [
{
"id": "8",
"scv_name": "practice 8",
"scv_description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.",
"scv_comments": "Lorem Ipsum is simply dummy text",
"scv_status": "1",
"scv_type": "1",
"scv_thumb": "1.jpg",
"scv_url": "1.mp4",
"id_player": "4",
"scv_createdby": "7",
"scv_created": "2018-07-18 00:00:00",
"scv_modifiedby": "7",
"scv_modified": "2018-07-20 00:00:00"
}
]
}
],
"image": []
}