当前位置: 首页 > 知识库问答 >
问题:

GSON应为BEGIN_数组,但为BEGIN_对象

狄令
2023-03-14

当我只收到列表中的一个项目时,就会出现这个错误。我在服务器端REST Web服务中使用Jersey,只有当列表返回一个元素时,以及当它有0个元素时,我才得到错误。lang.NullPointerException但当它有多个异常时,它工作得非常完美,我就是这样做的:

@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为什么不发送包含一个项目的列表?是泽西岛的虫子吗?有没有办法让它始终序列化为数组?

这是web服务返回0元素时的JSON数据:

null

这是web服务返回1个元素时的JSON数据:

{
    "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+2B9fX3r1atXP3777befW7du3aOpqanfjYGysjJFY2Oj4uzZszt6e3sxOjoqGBu/i7sTE8QkJia/EdC/aeRr3D9+9y7Gx8cxPDyM2tpaO821JysrayHP/Z0YuH37tuLcuXO7+vr6vpEN9LZa4DBcQ5vxGjpqrqOz5ga6TIT5Brpd6K27hZGBboyNjU2bdzgcdy5duvS7N954I4Ru8ciWLVu+XQMDAwO8AjvZAEe+v7kG2pyfQL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O1t87QQ+xpEruGIvwULDtXoX73CopwDEU4Cs37wtGaHgpHpgrt2UEkOADdtLw9+X7oLfBDP0VqgG5yu1iJ2yVKDJb6YPAw442hMq8pyj2J5YQHBo8wy3BbsBQDRH/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": "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"
    }
}

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

{
    "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/5a33nrrudLS0sePHj2qyMvLu9/A+N0J9JmvonE/CUtdBHvWUtgLvGEv9Ye9PHCKEj/Y871gz1wCe/IP0fnbtZAG+yCRgZGREdy5c0fAKUVpOUkrIrW0tAw3NzcPNzU1DTGUtoKGhgaBzWYTWK3WIdpPAovFIqirqxNQeg5TUPo+++yzQ+vXr3/0yJEjD1qBcYwO38bA1++g90IG+r7MRd+1QvTdLEFfVRn6Kg+j7+ti9F7JR+/FAz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LeRYHopIBYCpWdFzO9Ij7P9FIo+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/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4kAyZLlL9Vl+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+2B9fX3r1atXP3777befW7du3aOpqanfjYGysjJFY2Oj4uzZszt6e3sxOjoqGBu/i7sTE8QkJia/EdC/aeRr3D9+9y7Gx8cxPDyM2tpaO821JysrayHP/Z0YuH37tuLcuXO7+vr6vpEN9LZa4DBcQ5vxGjpqrqOz5ga6TIT5Brpd6K27hZGBboyNjU2bdzgcdy5duvS7N954I4Ru8ciWLVu+XQMDAwO8AjvZAEe+v7kG2pyfQLvTG7r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Q+xpEruGIvwULDtXoX73CopwDEU4Cs37wtGaHgpHpgrt2UEkOADdtLw9+X7oLfBDP0VqgG5yu1iJ2yVKDJb6YPAw442hMq8pyj2J5YQHBo8wy3BbsBQDRH/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": "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响应,如下所示:

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类:

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

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

我非常感谢你帮助解决这个问题。提前谢谢

共有3个答案

贺善
2023-03-14

我最终使用Jackson库而不是gson来解析json响应。它会自动将对象转换为ArrayList。就易用性而言,它与gson非常相似。希望有帮助!

太叔马鲁
2023-03-14

基本上,您的web服务在响应方式上是不可靠的。如果project只有一个对象,那么它只返回一个对象,而不是数组

MikO的答案解决了这个问题,但另一种方法是将该逻辑封装在自定义反序列化器中:

class MyDeserializer implements JsonDeserializer<ProjectContainer> {

    @Override
    public ProjectContainer deserialize(JsonElement je, Type type, JsonDeserializationContext jdc) throws JsonParseException {

        JsonObject jo = je.getAsJsonObject().getAsJsonObject("project");
        if (jo.isJsonArray()) {
             return new Gson().fromJson(je, ProjectContainer.class);
        } else {
             Project p = jdc.deserialize(jo, Project.class);
             List<Project> pList = new ArrayList<Project>(1);
             pList.add(p);
             ProjectContainer pc = new ProjectContainer();
             pc.setProjects(pList);
             return pc;
        }
    }
}

然后你可以使用:

Gson gson = new GsonBuilder()
                .registerTypeAdapter(ProjectContainer.class, new MyDeserializer())
                .build();
ProjectContainer pContainer = gson.fromJson(myJson, ProjectContainer.class);
刘弘新
2023-03-14

你的问题对我来说真的很奇怪。。。Jersey对单元素数组的JSON序列化似乎存在一些问题。。。如果你在谷歌上搜索“Jersey JSON single element Array”,你会发现同样的问题,比如这里或这里。我对泽西了解不多,所以我帮不了你。。。

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

//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

这样的东西应该对你有用,尽管显然最好的事情是修复序列化问题!

 类似资料:
  • 这个错误又回来了! 我是改装的初学者,我看过这篇文章:改装:预期BEGIN_OBJECT但BEGIN_ARRAY 但是即使我试着让它适应我的模型,我仍然不能让它工作。所以我的错误是: 我得到的是: 解析类的属性: IoTGenericDevice子类的属性: 改装电话: 经理:

  • 我知道以前有人问过很多这个问题,但在我的情况下,我找不到解决办法。我请求获得关于POJO(电影)列表的json回复 虽然这是我第一次使用改型,但我在标题中遇到了问题。我正在创建的url是正确的,下面是响应的示例: 这是我的模特 } 我的@GET 我的数据请求类 最后,我想在我的主要活动中做些什么

  • 我试图使用GSON解析一些非常简单的JSON。这是我的代码: 以下是从url返回的JSON: 我得到了一个例外: 有什么想法吗?我是格森的新手。

  • 我对gson反序列化有问题 是我儿子 我的kotlin小工具 还有我的反序列化函数 我有这个错误 我不明白什么是错误的,我真的觉得一切都是正确的,我做了多次不同的测试。 你能帮助我吗。

  • 您好,我是新来的改装库,我有问题解析一些json。我已经研究了Stackoverflow的一些其他解决方案,但我的问题没有太多的运气。我正在尝试一个简单的网络服务。如有任何建议,将不胜感激。。 json文件 请求方法 Employees.java 雇员萨皮。JAVA 我得到的错误是 我理解错误是说数据是一个对象,它应该以数组开头,但我无法计算出来。。谢谢

  • 使用改装库来使用一项服务,我为我的界面、模型以及如何使用界面添加了代码。 我不断收到一个GSON抛出的“预期的BEGIN_对象,但是BEGIN_数组” 界面 模范公立班学生{ 我怎么称呼方法