Spring 3 ArrayList 使用 Jackson 序列化为 JSON,无需包装到对象(无名称)
我有一个应该返回 JSON 数组的简单资源,但它返回的是数组中的对象:
@RequestMapping(value = "/types", method = RequestMethod.GET)
public List <JsonObject> types() {
ArrayList <JsonObject> list=new ArrayList<JsonObject>();
list.add(new JsonObject("Audi"));
list.add(new JsonObject("Mercedes"));
return list;
}
其中 JsonObject 是具有三个字符串属性(值、id、标签)的简单类。
返回:
{"jsonObjectList":[{"value":"Audi","id":"Audi","label":"Audi"},{"value":"Mercedes","id":"Mercedes","label":"Mercedes"}]}
但我需要什么(因为它是 Jquery UI 自动完成预期的):
[{"value":"Audi","id":"Audi","label":"Audi"},{"value":"Mercedes","id":"Mercedes","label":"Mercedes"}]
如何实现? 提前致谢。
I have simple resource which should return JSON array, but it returns object in which is array:
@RequestMapping(value = "/types", method = RequestMethod.GET)
public List <JsonObject> types() {
ArrayList <JsonObject> list=new ArrayList<JsonObject>();
list.add(new JsonObject("Audi"));
list.add(new JsonObject("Mercedes"));
return list;
}
Where JsonObject is simple class with three String atributes (value,id,label).
Returns:
{"jsonObjectList":[{"value":"Audi","id":"Audi","label":"Audi"},{"value":"Mercedes","id":"Mercedes","label":"Mercedes"}]}
But I what I need (because it's Jquery UI autocomplete expected):
[{"value":"Audi","id":"Audi","label":"Audi"},{"value":"Mercedes","id":"Mercedes","label":"Mercedes"}]
How to achieve that?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Spring 3.1 中,您应该能够在名为
extractValueFromSingleKeyModel
更改为true
以删除包装器。哦,似乎之前已经问过这个问题 为什么 Jackson 用一个以类命名的额外层来包装我的对象?
In Spring 3.1, you should be able to set a property on the
MappingJacksonJsonView
bean calledextractValueFromSingleKeyModel
totrue
to remove the wrapper.Oh, seems like this has been asked before Why is Jackson wrapping my objects with an extra layer named after the class?