GSON 应为 BEGIN_ARRAY,但为 BEGIN_OBJECT
Posted
技术标签:
【中文标题】GSON 应为 BEGIN_ARRAY,但为 BEGIN_OBJECT【英文标题】:GSON Expected BEGIN_ARRAY but was BEGIN_OBJECT 【发布时间】:2013-05-15 06:40:22 【问题描述】:当我只收到一个列表中的一项时,我收到了这个错误。我在服务器端 REST Web 服务中使用 Jersey,我只在 List 返回一个元素并且当它有 0 elements
我得到 java.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
当网络服务返回 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+vr6vp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AAAABJRU5ErkJggg==",
"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 数据:
"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+yCRgZGREdy5c0fAKUVpOUkrIrW0tAw3NzcPNzU1DTGUtoKGhgaBzWYTWK3WIdpPAovFIqirqxN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wRJ/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/JXbh9TrmirEDOpA4JQQ6kdCBDiQOU65UjNdO2djYGLCzKRJjY841SJo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zPtVUm1aiFSXHirVZ4RJ1swIyZYZKTVkRUkN2TFSoyCa6tyOIMIkW3aIZM0OluqzA4kAyZLlL9Vl+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+vr6vpEN9LZa4DBcQ5vxGjpqrqOz5ga6TIT5Brpd6K27hZGBboyNjU2bdzgcdy5duvS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edStartDate": "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;
非常感谢您帮助解决这个问题。提前致谢
【问题讨论】:
这可能是相关的:How to make Jersey/Jackson serialize empty list; single element list as an array. 问题很不清楚......当返回List
作为1个元素时你只会得到错误?当它有 0 或 >1 个元素时它可以工作吗?清除 JSON 响应也会很有用...
是的,我只在 List 返回一个元素并且它有 0 个元素时得到错误,但当它有多个元素时,它会完美运行。
您能否提供 Web 服务返回的实际 JSON 响应,如果可能的话,包括 3 个不同的响应(0、1 或 >1 个元素)
@MikO 我根据你的要求更新了我的问题
【参考方案1】:
你的问题结果对我来说真的很奇怪......看来泽西岛的单元素数组的 JSON 序列化肯定有问题......如果你谷歌“Jersey JSON 单元素数组”你'会发现同样的问题,例如here 或here。我对泽西岛了解不多,所以无法帮助您...
也就是说,我可以建议在 Gson 中使用手动解析的解决方法,以使您的解析适应 2 个不同的响应(对象或数组)。你可以这样做:
//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
。
这样的东西应该对你有用,虽然显然最好的办法是解决序列化问题!
【讨论】:
Jersey 或 Gson 不存在“问题”……Web 服务基本上已损坏且无法持续返回。他的 Java 类有一个List<Project>
,如果它要正确映射,它必须在 JSON(一个数组)中。 Web 服务根据 Project
对象的数量以两种不同的方式响应
@Brian,显然 Web 服务没有正确响应,这是肯定的,但如果到 JSON 的映射是由 Jersey 自动生成的......我想确实存在一些“问题”(或奇怪行为)在泽西映射中......否则,如果他没有任何序列化代码,错误在哪里?!服务哪里坏了?!问题一定出在 Jersey 的序列化中……
哦,等等,我明白你的意思了——我看错了。我以为你的意思是在 Gson 方面。未知 - 这取决于 getPagedProjects
返回的内容。
@Brian,是的,在我的回答中,我明确表示问题出在泽西岛......并且查看 JSON 响应,方法 getPagedProjects
对我来说似乎是正确返回的,否则他不会无法获取该数据...并且它必须返回 List<Project>
或者甚至 IDE 都会抱怨...所以,泽西岛再次出现问题,在我提供的链接中它们似乎有相同的问题...
谢谢。我非常感谢您对@MikO 的帮助,您提供的链接非常有帮助,我更喜欢修复 Jersey 的序列化问题,而不是解决 Gson 中的解析问题。再次感谢您,非常感谢您的帮助。【参考方案2】:
基本上,您的 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);
【讨论】:
【参考方案3】:我最终使用Jackson library 而不是gson
来解析json
响应。它会自动将对象转换为ArrayList
。就易用性而言,它与 gson 非常相似。希望对您有所帮助!
【讨论】:
以上是关于GSON 应为 BEGIN_ARRAY,但为 BEGIN_OBJECT的主要内容,如果未能解决你的问题,请参考以下文章
GSON:java.lang.IllegalStateException:应为 BEGIN_OBJECT 但为 STRING
com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:应为 BEGIN_OBJECT 但为 STRING
应为 BEGIN_ARRAY,但在第 1 行第 1 列路径为 STRING
java.lang.IllegalStateException:应为 BEGIN_ARRAY,但路径为 BEGIN_OBJECT