GSON解析会在解析空的JSONArray时引发异常

时间:2018-07-29 16:19:42

标签: android json gson

使用GSON解析整个JSON,并将其转换为相应的模型对象。 由于某些原因,如果JSONArray“视频”为空,则GSON会引发异常。 在解析JSONArray“视频”时,如果数组为空-([]),则解析将引发异常

  

com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_ARRAY,但位于第1行445列的$ .video路径上。

如果“视频”是一个空的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;
}

}

导致Json异常的原因是“视频”为空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": []
}

成功解析的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": []
}

2 个答案:

答案 0 :(得分:0)

null 定义更改为数组对象

@SerializedName("video_list")
@Expose
private List<VideoList> videoList = null;

更改为该代码

@SerializedName("video_list")
@Expose
private List<VideoList> videoList = new ArrayList<VideoList>();

我遇到了这个问题,并通过更改null定义来解决

答案 1 :(得分:0)

我无法复制该错误。因此,我建议仔细检查您返回的JSON字符串。从这个posting中,错误意味着返回的值是一个字符串,即“视频”:“ []” ,而不是“视频”:[] 。其他post显示了一个示例。