childrenjson数据递归_jquery–如何递归迭代json对象数组我试图出如何递归迭代这个JSON,⽆论我得到这些数据时有多少级别的嵌套,因为嵌套量可以是任何数量.我可以让它达到⼀个深度,但似乎⽆论我做什么,如果我想要⼀个额外的嵌套⽔平,我必须为⼉童进⾏测试,虽然这可能是正确的,但它似乎不是.
我想最终得到的是⼀个新的数据集,如果正在查看的当前元素有⼀个兄弟或者有⼀个孩⼦等,则添加了’next_id’.
我很困惑所以我希望这是有道理的.
我试过这个:
function visit(obj){
var msg = '';
for(var prop in obj){
if(typeof(obj[prop]) == 'object'){
if(String.call(obj[prop]) == '[object Array]'){
for(var i = 0; i < obj[prop].length; i++){
msg += "the element " + prop + " (array) was visited: " + obj[prop][i].key + "
";
if ( obj[prop][i].children )
{
msg += "the element " + prop + " (array-CHILDREN) was visited: " + obj[prop][i].key + "
";
for ( y=0;y
{
msg += "the element " + prop + " (array-HELLO) was visited: " + obj[prop][i].children[y].title + "
";
if ( obj[prop][i].children[y].children )
{
msg += "the element " + prop + " (array-HELLO-CHILD) was visited: " + obj[prop][i].children[y].title + "
";
$.each(obj[prop][i].children[y].children, function( index, value ) {
alert( "red alert " + value.title );
msg += "the element " + prop + " (CHILD-CHILD) was visited: " + i + ": " + index + ": " + value.title + "
";
visit(value);
});
}
}
}
}
}else{
msg += "the element " + prop + " (object) was visited" + "hello" + " ";
visit(obj[prop]);
}
}else{
msg += "the element " + prop + " = " + obj[prop] + " was visited" + " ";
}
}
return msg;
}
我称之为:
访问(d)其中“d”是以下数据.
在此先感谢您的帮助.
那么,这是我的数据:
{
"key": "root_1",
"title": "root",
"children": [
{
"key": "1",
"title": "Title Goes Here",
"data": {
"can_drag": false,
"can_drop": false
}
},
{
"folder": false,
"key": "_1",
"title": "Text for regular step goes here",
"data": {
"step_type": "span",
"imagePath": "images/",
"icon": "text_align_center.png",
"can_drag": true,
"can_drop": [
"before",
"after"
]
}
},
{
"folder": false,
"key": "_2",
"title": "Text for regular step goes here", "data": {
"step_type": "span",
"imagePath": "images/",
"icon": "text_align_center.png",
"can_drag": true,
"can_drop": [
"before",
"after"
]
}
},
{
"expanded": true,
"folder": true,
"key": "_3",
"title": "Fork question here",
"data": {
"step_type": "fork",
"imagePath": "images/",
"can_drag": true,
"can_drop": false
},
"children": [
{
"expanded": true,
"key": "_4",
"title": "branch 1 answer text", "data": {
"step_type": "fork_branch", "imagePath": "images/",
"icon": "arrow_right.png",
"can_drag": true,
"can_drop": false
},
"children": [
{
"key": "_5",
"title": "branch 1 answer text first step", "data": {
"imagePath": "images/",
"icon": "text_align_center.png",
"step_type": "step",
"can_drag": true,
"can_drop": false
}
},
{
"expanded": true,
"folder": true,
"key": "_8",
"title": "Fork question here",
"data": {
"imagePath": "images/",
"icon": "fork.gif",
"can_drag": true,
"can_drop": false
},
typeof array"children": [
{
"expanded": true,
"key": "_9",
"title": "branch 1 answer text", "data": {
"step_type": "fork_branch", "imagePath": "images/",
"icon": "arrow_right.png",
"can_drag": true,
"can_drop": false
},
"children": [
{
"key": "_10",
"title": "branch 1 answer text first step", "data": {
"imagePath": "images/",
"icon": "text_align_center.png",
"step_type": "step",
"can_drag": true,
"can_drop": false
}
},
{
"expanded": true,
"folder": true,
"key": "_13",
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论