Web項目開發中,由於前端框架的限制,很多時候需要將返回的數據加工後再返回給前端。最典型的就是返回一棵部門層級關係的樹。下面即是一棵帶有層級的文檔標題樹,和我們書籍的目錄結構一樣。
在樹的結構中,每一個節點都需要有一顆父級節點,即使是頂級節點,也需要給其父節點賦值。比如,在下面的帶有父子關係的線性結構中,有些id的parentId是null的,此時就需要將parentId爲null賦值爲projectId。同時使用了fastjson作爲輔助工具,具有一定的通用性。其數值如下:
"data": [
{
"id": "661661966154199040",
"text": null,
"parentId": null,//父級id爲空,需要將其賦值爲projectId
"orderCount": 0,
"projectId": "741917334711331456",
"children": null//下級節點
},
{
"id": "661662130646413312",
"text": null,
"parentId": null,
"orderCount": 1,
"projectId": "741917334711331456",
"children": null
},
{
"id": "661662428978868224",
"text": null,
"parentId": null,
"orderCount": 2,
"projectId": "741917334711331456",
"children": null
},
{
"id": "772919473459032064",
"text": null,
"parentId": "653294833572617088",
"orderCount": 2,
"projectId": "741917334711331456",
"children": null
},
{
"id": "772917879430574080",
"text": null,
"parentId": "653294833572617088",
"orderCount": 3,
"projectId": "741917334711331456",
"children": null
},
{
"id": "661661793088827392",
"text": null,
"parentId": null,
"orderCount": 4,
"projectId": "741917334711331456",
"children": null
},
{
"id": "661661918448185344",
"text": null,
"parentId": null,
"orderCount": 5,
"projectId": "741917334711331456",
"children": null
},
{
"id": "772919255229394944",
"text": null,
"parentId": null,
"orderCount": 6,
"projectId": "741917334711331456",
"children": null
},
{
"id": "661661956607963136",
"text": null,
"parentId": null,
"orderCount": 7,
"projectId": "741917334711331456",
"children": null
},
{
"id": "661661957128056832",
"text": null,
"parentId": null,
"orderCount": 8,
"projectId": "741917334711331456",
"children": null
},
{
"id": "653294833572617088",
"text": null,
"parentId": null,
"orderCount": 11,
"projectId": "741917334711331456",
"children": null
},
{
"id": "653370321808759680",
"text": null,
"parentId": null,
"orderCount": 11,
"projectId": "741917334711331456",
"children": null
},
{
"id": "653385876393532288",
"text": null,
"parentId": null,
"orderCount": 11,
"projectId": "741917334711331456",
"children": null
}
]
樹的DTO結構如下,注意children:
public class TreeDTO {
/** 兒子id **/
private Long id;
/** 附帶文本 **/
private String text;
/** 父級id **/
private Long parentId;
/** 排序值 **/
private Integer orderCount;
/** 項目id **/
private Long projectId;
/** 孩子節點 **/
private List<TreeDTO> children;
/**
* @return the text
*/
public String getText() {
return text;
}
/**
* @param text
*/
public void setText(String text) {
this.text = text;
}
/**
* @return the id
*/
public Long getId() {
return id;
}
/**
* @param id
*/
public void setId(Long id) {
this.id = id;
}
/**
* @return the parentId
*/
public Long getParentId() {
return parentId;
}
/**
* @param parentId
*/
public void setParentId(Long parentId) {
this.parentId = parentId;
}
/**
* @return the orderCount
*/
public Integer getOrderCount() {
return orderCount;
}
/**
* @param orderCount
*/
public void setOrderCount(Integer orderCount) {
this.orderCount = orderCount;
}
/**
* @return the projectId
*/
public Long getProjectId() {
return projectId;
}
/**
* @param projectId
*/
public void setProjectId(Long projectId) {
this.projectId = projectId;
}
/**
* @return the children
*/
public List<TreeDTO> getChildren() {
return children;
}
/**
* @param children
*/
public void setChildren(List<TreeDTO> children) {
this.children = children;
}
}
- 先將parentId爲空的節點設置爲projectId。
/**
* 將根目錄節點的設置爲projectId
*/
private void setRootTreeNode(List<TreeDTO> treeList, Long projectId) {
// 沒有parentId,即將projectId作爲parentId
for (TreeDTO dto : treeList) {
Long parentId = dto.getParentId();
if (parentId == null) {
dto.setParentId(projectId);
}
}
}
- 返回樹型結構
// 構造樹結構,treeList爲上面的JSON節點的List形式,返回之後即爲樹型
JSONArray resultTree = listInfoToTree(JSONArray.parseArray(JSON.toJSONString(treeList)), "id", "parentId", "children");
- 將文中開始列出的list轉爲tree
/**
* 將JSONArray數組轉爲樹狀結構
*
* @param arr
* 需要轉化的數組
* @param id
* 數據唯一的標識鍵值名稱
* @param pid
* 父id唯一標識鍵值名稱
* @param child
* 子節點名稱
*
* @return array
*/
private JSONArray listInfoToTree(JSONArray arr, String id, String pid, String child) {
JSONArray ret = new JSONArray();
// 將數據直接構建成爲hash
JSONObject hash = new JSONObject();
// 節點大小
int size = arr.size();
// 將數組轉爲Object的形式,key爲數組中的id,並組裝爲map
for (int i = 0; i < size; i++) {
JSONObject json = (JSONObject) arr.get(i);
hash.put(json.getString(id), json);
}
// 遍歷結果集
for (int j = 0; j < size; j++) {
// 單條記錄
JSONObject nodeVal = (JSONObject) arr.get(j);
// 在hash中取出key爲單條記錄中pid的值
JSONObject hashParent = (JSONObject) hash.get(nodeVal.get(pid).toString());
// 如果記錄的pid存在,則說明它有父節點,將她添加到孩子節點的集合中(這裏與剛纔所說的所有parentId爲空的節點填充一個projectid並不衝突,因爲當填入projectId的時候,hashParent是get不到值的)
if (hashParent != null) {
// 構造child屬性
if (hashParent.get(child) != null) {
JSONArray chArr = (JSONArray) hashParent.get(child);
chArr.add(nodeVal);
hashParent.put(child, chArr);
} else {
JSONArray chArr = new JSONArray();
chArr.add(nodeVal);
hashParent.put(child, chArr);
}
} else {
ret.add(nodeVal);
}
}
return ret;
}
- PostMan返回結果:
"data": [
{
"orderCount": 0,
"id": "661661966154199040",
"text": "迄病蚤莎紊臉液板鋅猿燈金",
"projectId": "741917334711331456",
"parentId": "741917334711331456"
},
{
"orderCount": 1,
"id": "661662130646413312",
"text": "雖曙拱抿蠱績簧蛛傘殺",
"projectId": "741917334711331456",
"parentId": "741917334711331456"
},
{
"orderCount": 2,
"id": "661662428978868224",
"text": "粳野贛下喉迎醇叢攘頹",
"projectId": "741917334711331456",
"parentId": "741917334711331456"
},
{
"orderCount": 4,
"id": "661661793088827392",
"text": "仰儡轄龍蒸錳篡帶傘螞",
"projectId": "741917334711331456",
"parentId": "741917334711331456"
},
{
"orderCount": 5,
"id": "661661918448185344",
"text": "殃書您衡牛閻農鳴",
"projectId": "741917334711331456",
"parentId": "741917334711331456"
},
{
"orderCount": 6,
"id": "772919255229394944",
"text": "154641",
"projectId": "741917334711331456",
"parentId": "741917334711331456"
},
{
"orderCount": 7,
"id": "661661956607963136",
"text": "賒於毋錢永患幾傷凜婆輝雪",
"projectId": "741917334711331456",
"parentId": "741917334711331456"
},
{
"orderCount": 8,
"id": "661661957128056832",
"text": "刀腦散郎蒙鈾餅每舍誓刺卷",
"projectId": "741917334711331456",
"parentId": "741917334711331456"
},
{
"children": [
{
"orderCount": 2,
"id": "772919473459032064",
"text": "154642",
"projectId": "741917334711331456",
"parentId": "653294833572617088"
},
{
"orderCount": 3,
"id": "772917879430574080",
"text": "15464",
"projectId": "741917334711331456",
"parentId": "653294833572617088"
}
],
"orderCount": 11,
"id": "653294833572617088",
"text": "測試鏈22",
"projectId": "741917334711331456",
"parentId": "741917334711331456"
},
{
"orderCount": 12,
"id": "653370321808759680",
"text": "sfdas",
"projectId": "741917334711331456",
"parentId": "741917334711331456"
},
{
"orderCount": 13,
"id": "653385876393532288",
"text": "哈哈哈哈哈哈哈哈",
"projectId": "741917334711331456",
"parentId": "741917334711331456"
}
]