[{"roleId":1,"roleName":"system Admin","createUser":"1","createActive":"N"},{"roleId":2,"roleName":"admin","createUser":"1","createActive":"Y"},{"roleId":3,"roleName":"system Admin","createUser":"1","createActive":"Y"}]
这是我使用list在spring中编写的输出,但是我希望它以嵌套的json格式存在,就像树视图(结构)一样,如下面的结构。
[{ "id": 1, "data": "Category", "attr": { "id": "1", "selected": false }, "children":
[
{ "id": 2, "data": "SubCategory1", "attr": { "id": "2", "selected": false }, "children":
[{ "id": 4, "data": "Item1", "attr": { "id": "4", "selected": false }, "children": [] },
{ "id": 5, "data": "Item2", "attr": { "id": "5", "selected": false }, "children": [] },
{ "id": 6, "data": "Item3", "attr": { "id": "6", "selected": false }, "children": [] }
]
},
{ "id": 3, "data": "SubCategory2", "attr": { "id": "3", "selected": false }, "children":
[{ "id": 7, "data": "Item4", "attr": { "id": "7", "selected": false }, "children": [] },
{ "id": 8, "data": "Item5", "attr": { "id": "8", "selected": false }, "children": [] },
{ "id": 9, "data": "Item6", "attr": { "id": "9", "selected": false }, "children": [] }
]
}
]
}];
我的代码是,
@GET
@Path("/findall")
public List<UserRole> getAll() {
return userroleservice.findAll();
}
答案 0 :(得分:0)
使用像Gson
这样的Json解析器的漂亮打印Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(obj);
System.out.println(json);
如果是在网页中使用javascript或Jquery format plugin
var str = JSON.stringify(obj, null, 4);