js或者Java 遍历json数组 实现如下功能 跪求大神
js或者java,已知json数组a如下a是个动态json数组,只知道根节点id为“0000”还知道里面有个节点为parent_id(0000是其它节点得parent_i...
js或者java,已知json数组a如下
a是个动态json数组,只知道根节点id为“0000” 还知道里面有个节点为parent_id(0000是其它节点得parent_id有这样的关系),其它属性(除a,还有其它节点)不知道
想根据id和parent_id关系得到json数组b
举例:
a=[ {
"a" : "5",
"id" : "0000",
"parent_id" : "null"
}, {
"a" : "1",
"id" : "1000",
"parent_id" : "0000"
}, {
"a" : "2",
"id" : "2000",
"parent_id" : "0000"
}, {
"a" : "6",
"id" : "1001",
"parent_id" : "1000"
}, {
"a" : "6",
"id" : "1002",
"parent_id" : "1000"
}, {
"a" : "23",
"id" : "2001",
"parent_id" : "2000"
}, {
"a" : "11",
"id" : "2002",
"parent_id" : "2000"
} ];
b=[{
"id" : "0000",
"parent_id" : "null",
"a" : "5",
"children" : [ {
"id" : "1000",
"parent_id" : "0000",
"a" : "1",
"children" : [ {
"id" : "1001",
"parent_id" : "1000",
"a" : "6"
}, {
"id" : "1002",
"parent_id" : "1000",
"a" : "6"
} ]
}, {
"id" : "2000",
"parent_id" : "0000",
"a" : "2",
"children" : [ {
"id" : "2001",
"parent_id" : "2000",
"a" : "23"
}, {
"id" : "2002",
"parent_id" : "2000",
"a" : "11"
} ]
} ]
}] 展开
a是个动态json数组,只知道根节点id为“0000” 还知道里面有个节点为parent_id(0000是其它节点得parent_id有这样的关系),其它属性(除a,还有其它节点)不知道
想根据id和parent_id关系得到json数组b
举例:
a=[ {
"a" : "5",
"id" : "0000",
"parent_id" : "null"
}, {
"a" : "1",
"id" : "1000",
"parent_id" : "0000"
}, {
"a" : "2",
"id" : "2000",
"parent_id" : "0000"
}, {
"a" : "6",
"id" : "1001",
"parent_id" : "1000"
}, {
"a" : "6",
"id" : "1002",
"parent_id" : "1000"
}, {
"a" : "23",
"id" : "2001",
"parent_id" : "2000"
}, {
"a" : "11",
"id" : "2002",
"parent_id" : "2000"
} ];
b=[{
"id" : "0000",
"parent_id" : "null",
"a" : "5",
"children" : [ {
"id" : "1000",
"parent_id" : "0000",
"a" : "1",
"children" : [ {
"id" : "1001",
"parent_id" : "1000",
"a" : "6"
}, {
"id" : "1002",
"parent_id" : "1000",
"a" : "6"
} ]
}, {
"id" : "2000",
"parent_id" : "0000",
"a" : "2",
"children" : [ {
"id" : "2001",
"parent_id" : "2000",
"a" : "23"
}, {
"id" : "2002",
"parent_id" : "2000",
"a" : "11"
} ]
} ]
}] 展开
3个回答
展开全部
/**
* json格式转树状结构
* @param {json} json数据
* @param {String} id的字符串
* @param {String} 父id的字符串
* @param {String} children的字符串
* @return {Array} 数组
*/
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;
}
以上方法完全可以满足你的 需求,Json属性结构的转换。
调用方法:
var a=[ {
"a" : "5",
"id" : "0000",
"parent_id" : "null"
}, {
"a" : "1",
"id" : "1000",
"parent_id" : "0000"
}, {
"a" : "2",
"id" : "2000",
"parent_id" : "0000"
}, {
"a" : "6",
"id" : "1001",
"parent_id" : "1000"
}, {
"a" : "6",
"id" : "1002",
"parent_id" : "1000"
}, {
"a" : "23",
"id" : "2001",
"parent_id" : "2000"
}, {
"a" : "11",
"id" : "2002",
"parent_id" : "2000"
} ];
var b=transData(a,"id","parent_id","children");
那么b就是你需要的结构。transData方法在以后遇到类似结构的数据,都可以使用!
以上的所有方法都是JavaScript !
更多追问追答
追问
请问 这种树形可以控制找几层?
比如我只想得到这样就行了 b=[{"id" : "0000","parent_id" : "null","a" : "5",
"children" : [ {"id" : "1000","parent_id" : "0000","a" : "1"}, {"id" : "2000","parent_id" : "0000","a" : "2"} ]}]不继续往下找了
追答
这样也是可以做到的 你要修改transData 函数。就可以!
我现在来不及写!晚上我看下!
展开全部
var b=[];
var createRoot = function(){
for(var i=0;i< a.length;i++) {
if (a[i].parent_id == "null") {
b.push(a[i]);
}
}
};
var buildTree = function(parent){
for(var i=0;i< a.length;i++){
if(a[i].parent_id == parent.id){
parent.children = parent.children||[];
parent.children.push(a[i]);
}
}
if(!parent.children){
return;
}
for(i=0;i< parent.children.length;i++){
buildTree(parent.children[i]);
}
};
createRoot();
for(var i=0;i< b.length;i++){
buildTree(b[i]);
}
console.log(b);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2015-08-06
展开全部
这个类似树形菜单的json格式,树形菜单数据保存后,根据parent_id来查找菜单之间的关系。你可以看看别人菜单显示时怎么做的。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询