【发布时间】:2018-07-29 16:19:41
【问题描述】:
整个 JSON 被解析并使用 GSON 转换为其对应的模型对象。
由于某些原因,如果JSONArray“视频”为空,GSON 会抛出异常。
解析JSONArray“video”时,如果数组为空-([]),则解析会抛出异常
com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:应为 BEGIN_ARRAY,但在第 1 行第 445 列路径 $.video 处为 STRING。
如果“视频”是一个空的 JsonArray,我需要的是指向该 jsonarray 的模型对象需要为空。
视频模型对象:
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;
}
}
由于“视频”为空 JSONArray 导致异常的 Json:
{
"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": []
}
解析成功的Json
{
"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": []
}
【问题讨论】:
-
您的代码在我看来没问题。尝试清理项目,你可以去 build >> clean project