GSON 应为 BEGIN_ARRAY,但为 BEGIN_OBJECT

2022-01-21 00:00:00 json android java jersey gson

当我只收到一个列表中的一项时,我收到了这个错误.我在服务器端 REST Web 服务中使用 Jersey,我只在 List 返回一个元素并且它有 0 个元素 时得到错误,我得到 java.lang.NullPointerException 但是,当它有多个时,它可以完美运行,这就是我的做法:

I'm getting this error when I receive only one single item in a list. I'm using Jersey in the server side REST Web service, I only get the error when the List returned one single element and when it has 0 elements I get java.lang.NullPointerException But when it has more than one it works perfectly, and this is how I'm doing it :

@GET
@Path("getproject")
@Produces(MediaType.APPLICATION_JSON)
public List<Project> getPagedProjects(
       @QueryParam("offset") int offset,
       @QueryParam("limit") int limit,
       @QueryParam("searchKey") String searchKey) throws Exception {

  System.out.println("Returning Paged Project ");
  return projectService.getPagedProjects(offset, limit, searchKey);
}

为什么 Jersey 不发送包含单个项目的列表?这是球衣上的虫子吗?有没有办法让它总是序列化为数组?

Why doesn't Jersey send a list that contains one single item? It is a bug in jersey? Is there a way to make it always serialize as an array?

这是我在 Web 服务返回 0 元素时的 JSON 数据:

This is my JSON data when the web service returns 0 element:

null

当网络服务返回 1 个元素时,这是我的 JSON 数据:

And This is my JSON data when web service returns 1 element :

{
    "project": {
        "abbreviation": "abc",
        "customer": "customer1",
        "description": "description3",
        "icon": "iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAN4UlEQVR42u2ZeXBUx53Hx8FgSAIWMiB0jO7RLRkwbFxxnC1ntyp/JXbh9TrmirEDOpA4JQQ6kdCBDiQOU65UjNdO2djYGLCzKRJjY841SJoZzaEZaTSjc0b3hST0JCH5u79fa54YCZLUZteurQpTfKq7X/f0+35//et+b4RC8fDz8PPw8/Dz8PMP/ykoKFBkZmY+evLkyR9//PHHr545c2azC6/9BUQ/jd/w4Ycf/uLNN9+M3r179w8AKNzd3b878eXl5QoSoXjnnXd+ZjAYmjo7Oye6uromme7u7m+coKenR+YbF3jcWHt7+2B9fX3r1atXP3777befW7du3aO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MsJER3wkVbHCy/tmZrJsmLP35H4XuUXos+eW8x+ZHLJg/f6H4E/u8efPmzJ079/u0GnyBWeQs/18yd+68hRT0+aT7ew//g/Dh5x/989/YnyApxuVsjAAAAABJRU5ErkJggg==",
        "name": "projectname3",
        "plannedEndDate": "2012-05-23T00:00:00+01:00",
        "plannedStartDate": "2012-05-23T00:00:00+01:00",
        "projectStatus": {
            "name": "In Progress"
        },
        "realEndDate": "2012-05-23T00:00:00+01:00",
        "realStartDate": "2012-05-23T00:00:00+01:00"
    }
}

当网络服务返回超过 1 个元素时,这是我的 JSON 数据:

And This is my JSON data when web service returns more than 1 element :

{
    "project": [
        {
            "abbreviation": "abd",
            "customer": "customer1",
            "description": "description1",
            "icon": "iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAN4UlEQVR42u2ZeXBUx53Hx8FgSAIWMiB0jO7RLRkwbFxxnC1ntyp/JXbh9TrmirEDOpA4JQQ6kdCBDiQOU65UjNdO2djYGLCzKRJjY841SJoZzaEZaTSjc0b3hST0JCH5u79fa54YCZLUZteurQpTfKq7X/f0+35//et+b4RC8fDz8PPw8/Dz8PMP/ykoKFBkZmY+evLkyR9//PHHr545c2azC6/9BUQ/jd/w4Ycf/uLNN9+M3r179w8AKNzd3b878eXl5QoSoXjnnXd+ZjAYmjo7Oye6uromme7u7m+coKenR+YbF3jcWHt7+2B9fX3r1atXP3777befW7du3aOpqanfjYGysjJFY2Oj4uzZszt6e3sxOjoqGBu/i7sTE8QkJia/EdC/aeRr3D9+9y7Gx8cxPDyM2tpaO821JysrayHP/Z0YuH37tuLcuXO7+vr6vpEN9LZa4DBcQ5vxGjpqrqOz5ga6TIT5Brpd6K27hZGBboyNjU2bdzgcdy5duvS7N954I4Ru8ciWLVu+XQMDAwO8AjvZAEe+v7kG2pyfQLvTG7rdSuj3+MKQ4gdjqr+gJtUPpr2+MKcoUZushP10CkZHhoV4SZIENNeEXq+vvnDhwqFTp05tfPfdd1+gNP3l/5a33n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PAw442hMq8pyj2J5YQHBo8wy3BbsBQDRH/5MvQRvWUe6C7zRNdhL3SWeqOjhFaM5rQd8hq6nrfs2Ru5HorpTUwGktiA8dJJGPf/CJZdq1G/5ynK55VoTI1BC71d2jPC0J4VQqKDSXQgiQ5AHy3pwCF/EuyPwRJ/DJX6Y/iwP+6U+WOknDjiD4k56kf4QjrG+BDeGDnmhZHjywkP3Dm+DMOCpRg8thS3iYGjZISM9ZLZHjLeRYHopIBYCpWdFzO9Ij7P9FIo+M9zly9fZgNxJtoD2j8chyl1DazJT9FGXEkRj6aIR1DEQ9FxQIWu3CD05AWS8AAMFJLoogAMlQRguDQAI4cDIZUFYbQ8GGNHVEQIxo+GTnGM6sdURBDGjwcQfhg7riS8MXrcExKZGCEDd8gAM3RsCQaP8orwalC6lVF60SqyAWNOkPmjXdE+Z5IjFYpHHnmE/8Y7JyUl5blz5z81/fH3x6Qvdv5Y+q9tMdLNpCjp1vYIqXJnuKTeFSpp94RIumSVpE9RSYa9wVJNarBk2qeSzPtVUm1aiFSXHirVZ4RJ1swIyZYZKTVkRUkN2TFSoyCa6tyOIMIkW3aIZM0OluqzA4kAyZLlL9Vl+UrmTF/JlKmUTBlKyUgY0pWSPt1Xqk7zk7T7/aVbe4PunI6NPLlS5fME61bMmTPne4899pj7ggULVq9Zs+bVtS+8sO+lf3k67VfPhKczrzA/CUtf58L6Z++xQeanU2z8abhgE/PPMhEudSZMsJER3wkVbHCy/tmZrJsmLP35H4XuUXos+eW8x+ZHLJg/f6H4E/u8efPmzJ079/u0GnyBWeQs/18yd+68hRT0+aT7ew//g/Dh5x/989/YnyApxuVsjAAAAABJRU5ErkJggg==",
            "name": "projectname1",
            "plannedEndDate": "2012-05-25T00:00:00+01:00",
            "plannedStartDate": "2012-05-23T00:00:00+01:00",
            "projectStatus": {
                "name": "Opened"
            },
            "realEndDate": "2012-05-25T00:00:00+01:00",
            "realStartDate": "2012-05-23T00:00:00+01:00"
        },
        {
            "abbreviation": "bd",
            "customer": "customer1",
            "description": "description2",
            "icon": "iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAN4UlEQVR42u2ZeXBUx53Hx8FgSAIWMiB0jO7RLRkwbFxxnC1ntyp/JXbh9TrmirEDOpA4JQQ6kdCBDiQOU65UjNdO2djYGLCzKRJjY841SJoZzaEZaTSjc0b3hST0JCH5u79fa54YCZLUZteurQpTfKq7X/f0+35//et+b4RC8fDz8PPw8/Dz8PMP/ykoKFBkZmY+evLkyR9//PHHr545c2azC6/9BUQ/jd/w4Ycf/uLNN9+M3r179w8AKNzd3b878eXl5QoSoXjnnXd+ZjAYmjo7Oye6uromme7u7m+coKenR+YbF3jcWHt7+2B9fX3r1atXP3777befW7du3aOpqanfjYGysjJFY2Oj4uzZszt6e3sxOjoqGBu/i7sTE8QkJia/EdC/aeRr3D9+9y7Gx8cxPDyM2tpaO821JysrayHP/Z0YuH37tuLcuXO7+vr6vpEN9LZa4DBcQ5vxGjpqrqOz5ga6TIT5Brpd6K27hZGBboyNjU2bdzgcdy5duvS7N954I4Ru8ciWLVu+XQMDAwO8AjvZAEe+v7kG2pyfQLvTG7rdSuj3+MKQ4gdjqr+gJtUPpr2+MKcoUZushP10CkZHhoV4SZIENNeEXq+vvnDhwqFTp05tfPfdd1+gNP3l/5a33nrrudLS0sePHj2qyMvLu9/A+N0J9JmvonE/CUtdBHvWUtgLvGEv9Ye9PHCKEj/Y871gz1wCe/IP0fnbtZAG+yCRgZGREdy5c0fAKUVpOUkrIrW0tAw3NzcPNzU1DTGUtoKGhgaBzWYTWK3WIdpPAovFIqirqxNQeg5TUPo+++yzQ+vXr3/0yJEjD1qBcYwO38bA1++g90IG+r7MRd+1QvTdLEFfVRn6Kg+j7+ti9F7JR+/FAzQmG0OWa9PiZQMsfmhoSDA4OCigVAXdC/39/bxCbG76cOCDgg4E0CGCjo4O0MGAtrY2AQUAdrsdra2tAjosqnJycpbyKggDNOE9A5TLY2Pj4JVgeJPOwHmdU43HjRLS6NgD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o+GTnGM6sdURBDGjwcQfhg7riS8MXrcExKZGCEDd8gAM3RsCQaP8orwalC6lVF60SqyAWNOkPmjXdE+Z5IjFYpHHnmE/8Y7JyUl5blz5z81/fH3x6Qvdv5Y+q9tMdLNpCjp1vYIqXJnuKTeFSpp94RIumSVpE9RSYa9wVJNarBk2qeSzPtVUm1aiFSXHirVZ4RJ1swIyZYZKTVkRUkN2TFSoyCa6tyOIMIkW3aIZM0OluqzA4kAyZLlL9Vl+UrmTF/JlKmUTBlKyUgY0pWSPt1Xqk7zk7T7/aVbe4PunI6NPLlS5fME61bMmTPne4899pj7ggULVq9Zs+bVtS+8sO+lf3k67VfPhKczrzA/CUtf58L6Z++xQeanU2z8abhgE/PPMhEudSZMsJER3wkVbHCy/tmZrJsmLP35H4XuUXos+eW8x+ZHLJg/f6H4E/u8efPmzJ079/u0GnyBWeQs/18yd+68hRT0+aT7ew//g/Dh5x/989/YnyApxuVsjAAAAABJRU5ErkJggg==",
            "name": "projectname2",
            "plannedEndDate": "2012-05-23T00:00:00+01:00",
            "plannedStartDate": "2012-05-23T00:00:00+01:00",
            "projectStatus": {
                "name": "Closed"
            },
            "realEndDate": "2012-05-23T00:00:00+01:00",
            "realStartDate": "2012-05-23T00:00:00+01:00"
        },
        {
            "abbreviation": "abc",
            "customer": "customer1",
            "description": "description3",
            "icon": "iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAN4UlEQVR42u2ZeXBUx53Hx8FgSAIWMiB0jO7RLRkwbFxxnC1ntyp/JXbh9TrmirEDOpA4JQQ6kdCBDiQOU65UjNdO2djYGLCzKRJjY841SJoZzaEZaTSjc0b3hST0JCH5u79fa54YCZLUZteurQpTfKq7X/f0+35//et+b4RC8fDz8PPw8/Dz8PMP/ykoKFBkZmY+evLkyR9//PHHr545c2azC6/9BUQ/jd/w4Ycf/uLNN9+M3r179w8AKNzd3b878eXl5QoSoXjnnXd+ZjAYmjo7Oye6uromme7u7m+coKenR+YbF3jcWHt7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xQeanU2z8abhgE/PPMhEudSZMsJER3wkVbHCy/tmZrJsmLP35H4XuUXos+eW8x+ZHLJg/f6H4E/u8efPmzJ079/u0GnyBWeQs/18yd+68hRT0+aT7ew//g/Dh5x/989/YnyApxuVsjAAAAABJRU5ErkJggg==",
            "name": "projectname3",
            "plannedEndDate": "2012-05-23T00:00:00+01:00",
            "plannedStartDate": "2012-05-23T00:00:00+01:00",
            "projectStatus": {
                "name": "In Progress"
            },
            "realEndDate": "2012-05-23T00:00:00+01:00",
            "realStartDate": "2012-05-23T00:00:00+01:00"
        }
    ]
}

在 android 端,我像这样反序列化 JSON 响应:

And in android side I deserialize the JSON response like this :

Gson gson = new Gson();
final ProjectContainer container = gson.fromJson(resultat, ProjectContainer.class);
final ListView lv = (ListView) findViewById(R.id.list);
adaptateur = new ProjectAdapter(ProjectActivity.this, R.layout.ligne_project, container);
lv.setAdapter(adaptateur);

这是我的 ProjectContainer 类:

This is my ProjectContainer class :

  public class ProjectContainer {
        @SerializedName("project")
        List<Project> projects ;

        public List<Project> getProjects() {
            return projects;
        }    
        public void setProjects(List<Project> projects) {
            this.projects = projects;
        }
    }

非常感谢您帮助解决这个问题.提前致谢

I would greatly appreciate your help to solve this problem. Thanks in advance

推荐答案

你的问题结果对我来说真的很奇怪......看来Jersey的单元素数组的JSON序列化肯定有问题......如果你谷歌"Jersey JSON 单元素数组"你会发现同样的问题,比如 这里或这里.我对泽西岛了解不多,所以无法帮助您...

Your issue results really weird for me... it seems that there must be some problem with Jersey's JSON serialization of single element arrays... if you Google "Jersey JSON single element arrays" you'll find the same issue, like here or here. I don't know too much about Jersey, so I can't help you with that...

也就是说,我可以建议在 Gson 中使用手动解析的解决方法,以使您的解析适应 2 个不同的响应(对象或数组).你可以这样做:

That said, I can suggest a workaround using manually parsing in Gson, to adapt your parsing to the 2 different responses (object or array). You could do something like this:

//manually parsing until get the "project" element...
JsonParser parser = new JsonParser();
JsonObject rootObejct = parser.parse(yourJsonString).getAsJsonObject();
JsonElement projectElement = rootObejct.get("project");

Gson gson = new Gson();
List<Project> projectList = new ArrayList<>();

//Check if "project" element is an array or an object and parse accordingly...
if (projectElement.isJsonObject()) {
    //The returned list has only 1 element
    Project project = gson.fromJson(projectElement, Project.class);
    projectList.add(project);
}
else if (projectElement.isJsonArray()) {
    //The returned list has >1 elements
    Type projectListType = new TypeToken<List<Project>>() {}.getType();
    projectList = gson.fromJson(projectElement, projectListType);
}

//Now you have a List<Project> projectList with one or many Project elements, 
//depending on the response...

请注意,您不需要您的类 ProjectContainer.

Note that you don't need your class ProjectContainer.

这样的东西应该对你有用,虽然显然最好的办法是解决序列化问题!

Something like this should work for you, although obviously the best thing would be fix the serialization issue!

相关文章