我在Java中有一些对象列表,我希望一起使用Jackson 2.2.2在json中编码:
List<Department> departments = this.departmentService.listAll();
List<Role> roles = this.roleService.listAll();
我希望得到一个看起来像这样的json:
[
"departments": [
{
"departmentId": 1,
"otherKey" : otherValue
},
{ ... }
],
"roles": [
{
"roleId": 1,
"otherKey" : otherValue
}
]
]
我不确定这个Json是否完美,但我想你明白了 有办法吗?
谢谢!
答案 0 :(得分:2)
Map<String, Object> map = new HashMap<String, Object>();
map.put("departments", departments);
map.put("roles", roles);
然后你可以将地图序列化为json