json轉json樹狀結構
在寫Restful服務的時候,因為都是返回去的都是一條條的json物件,這些都物件之間又是有關係的,但是卻沒有很好的體現出來,返回到頁面的時候,不能成為一個樹。返回去的json串資料如下:
[{"belongsname":"","id":901,"isleaf":0,"name":"XJBHX-2標專案部","pid":"","type":""},{"belongsname":"","id":902,"isleaf":1,"name":"綜合部(辦公室)","pid":"901","type":""},{"belongsname":"","id":903,"isleaf":1,"name":"工程部(工技部/技術部)","pid":"901","type":""},{"belongsname":"","id":904,"isleaf":1,"name":"安質部","pid":"901","type":""},{"belongsname":"","id":905,"isleaf":1,"name":"計財部","pid":"901","type":""},{"belongsname":"","id":906,"isleaf":1,"name":"物設部(物機部)","pid":"901","type":""},{"belongsname":"","id":907,"isleaf":1,"name":"中心試驗室","pid":"901","type":""}]
而我們在前臺介面上需要的資料可能是這樣的:
[
{
"belongsname": "",
"id": 901,
"isleaf": 0,
"name": "XJBHX-2標專案部",
"pid": "",
"type": ""
"children":[ {
"belongsname": "",
"id": 902,
"isleaf": 1,
"name": "綜合部(辦公室)",
"pid": "901",
"type": ""
},
{
"belongsname": "",
"id": 903,
"isleaf": 1,
"name": "工程部(工技部/技術部)",
"pid": "901",
"type": ""
},
{
"belongsname": "",
"id": 904,
"isleaf": 1,
"name": "安質部",
"pid": "901",
"type": ""
},
{
"belongsname": "",
"id": 905,
"isleaf": 1,
"name": "計財部",
"pid": "901",
"type": ""
},
{
"belongsname": "",
"id": 906,
"isleaf": 1,
"name": "物設部(物機部)",
"pid": "901",
"type": ""
},
{
"belongsname": "",
"id": 907,
"isleaf": 1,
"name": "中心試驗室",
"pid": "901",
"type": ""
}]
]
怎麼轉換呢?看示例:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript">
//將json串轉換成樹形結構
function transData(a, idStr, pidStr, chindrenStr) {
var r = [], hash = {}, id = idStr, pid = pidStr, children = chindrenStr, i = 0, j = 0, len = a.length;
for (; i < len; i++) {
hash[a[i][id]] = a[i];
}
for (; j < len; j++) {
var aVal = a[j], hashVP = hash[aVal[pid]];
if (hashVP) {
!hashVP[children] && (hashVP[children] = []);
hashVP[children].push(aVal);
} else {
r.push(aVal);
}
}
return r;
}
//測試方法
function test() {
//json串
var jsonData = eval('[{"belongsname":"","id":901,"isleaf":0,"name":"XJBHX-2標專案部","pid":"","type":""},{"belongsname":"","id":902,"isleaf":1,"name":"綜合部(辦公室)","pid":"901","type":""},{"belongsname":"","id":903,"isleaf":1,"name":"工程部(工技部/技術部)","pid":"901","type":""},{"belongsname":"","id":904,"isleaf":1,"name":"安質部","pid":"901","type":""},{"belongsname":"","id":905,"isleaf":1,"name":"計財部","pid":"901","type":""},{"belongsname":"","id":906,"isleaf":1,"name":"物設部(物機部)","pid":"901","type":""},{"belongsname":"","id":907,"isleaf":1,"name":"中心試驗室","pid":"901","type":""}]');
//繫結的欄位
var jsonDataTree = transData(jsonData, 'id', 'pid', 'chindren');
console.log(jsonDataTree);
}
//窗體載入執行的方法
window.onload = test;
</script>
</head>
</head>
<body>
</body>
</html>
剛開始做的時候,想複雜了,想著自己去遍歷然後手動拼寫json串,網路永遠做著我們先做的事。
相關文章
- Go 結構體 Json 互轉Go結構體JSON
- 目錄樹轉 JSONJSON
- Golang操作結構體、Map轉化為JSONGolang結構體JSON
- TreeView樹狀結構View
- Swift 使用JSON資料結構SwiftJSON資料結構
- 巢狀的map轉json,怎麼整,求助巢狀JSON
- Json物件與Json字串互轉JSON物件字串
- json總結JSON
- PHP 陣列轉樹結構/樹結構轉陣列PHP陣列
- js把json字串轉成json物件JSON字串物件
- json轉化JSON
- JSON轉ExcelJSONExcel
- 轉json操作JSON
- json結構體裡邊的刪除JSON結構體
- 標準化/結構化 JSON 輸出JSON
- 用中值排序基數法實現樹狀結構 (轉)排序
- PPT製作樹狀結構圖
- 資料結構——樹狀陣列資料結構陣列
- json字串和json格式物件的轉換JSON字串物件
- jOrgChart橫向的組織機構樹--手工構造json資料GCJSON
- Map 轉json資料,json資料轉換為MapJSON
- Python .get 巢狀 JSON 值Python巢狀JSON
- Java map轉JSONJavaJSON
- excel轉json操作ExcelJSON
- Json轉換(一)JSON
- Json轉換(二)JSON
- Json轉換(三)JSON
- 樹狀的資料結構的建立資料結構
- 關於elementUI樹狀結構的bugUI
- CSS 實現樹狀結構目錄CSS
- tkinter中樹狀結構的建立(十四)
- 論壇樹狀結構的展現
- 使用@ResponseBody物件轉json和@RequestBody進行json轉物件案例物件JSON
- cJSON:構建JSONJSON
- Antd:Tree樹形控制元件資料解析(JSON轉換)控制元件JSON
- 高效遍歷匹配Json資料,避免巢狀迴圈[轉]JSON巢狀
- JS操作JSON總結JSON
- Python3 解析複雜結構的 jsonPythonJSON