色婷婷狠狠18禁久久YY,CHINESE性内射高清国产,国产女人18毛片水真多1,国产AV在线观看

asp遞歸遍歷樹形json

劉姿婷1年前9瀏覽0評論

ASP遞歸遍歷樹形JSON

代碼示例:
function getTreeData(obj,dataid){
var rootData="",subData="",tempData="";
for(var i=0;i<obj.length;i++){
if(obj[i].parentid==dataid){
tempData='{"text": "'+obj[i].text+'","id":"' + obj[i].id + '","items": [';
subData += tempData + getTreeData(obj,obj[i].id) + ']},';
}
}
if(subData.length>0){
rootData=subData.substring(0,subData.length-1);
}
return rootData;
}
var jsonString='[{"id":"1","text":"A","parentid":"0"},{"id":"2","text":"B","parentid":"0"},{"id":"3","text":"C","parentid":"1"},{"id":"4","text":"D","parentid":"1"},{"id":"5","text":"E","parentid":"4"},{"id":"6","text":"F","parentid":"2"},{"id":"7","text":"G","parentid":"2"}]';
var jsonData = JSON.parse(jsonString);
var resultData = '[' + getTreeData(jsonData,0) + ']';
console.log(resultData);
說明:將JSON字符串解析成對象后,通過遞歸循環(huán)解析每個節(jié)點,將子節(jié)點作為items屬性添加到父節(jié)點對象中,最終組合成完整的樹形數(shù)據(jù)。