JS 递归 循环未完成就被RETURN出来的问题.

zbasic 2013-12-24 01:57:05
在使用JS做递归的树结构时,函数中的FOR被自己给return出来了,导致循环没有完成


var json={
"type":"class",
"text": "root",
"children": [
{
"type":"class",
"text": "children1",
"children": [
{
"type":"class",
"text": "children1_1",
"children": [
{
"type":"value",
"text": "children1_1_value1"
},
{
"type":"value",
"text": "children1_1_value2"
}
]
}
]
},
{
"type":"class",
"text": "children2",
"children": [
{
"type":"value",
"text": "children2_value1"
},
{
"type":"value",
"text": "children2_value2"
}
]
}
]
};

var xml="<root><class value='root'>";
xml += toxml(json['children']);
xml += "</class></root>";

function toxml(json){
var xml_str="";
for(i=0;i<json.length;i++){
if(json[i]["type"]=="class"){
xml_str += "<class value='"+json[i]['text']+"'>";
xml_str += toxml(json[i]['children']);
xml_str += "</class>";
}else{
xml_str += "<val>"+json[i]['text']+"</val>";
}

}


return xml_str;
}
alert(xml);

这里得出的结果是,究其原因是在"children1"节点循环的时候调用了其本身函数,函数有个return,导致了FOR循环的跳出.导致children2没有循环到.
<root>
<class value="root">
<class value="children1">
<class value="children1_1">
<val>children1_1_value1</val>
<val>children1_1_value2</val>
</class>
</class>
</class>
</root>

我想要得到的结果是
<root>
<class value="root">
<class value="children1">
<class value="children1_1">
<val>children1_1_value1</val>
<val>children1_1_value2</val>
</class>
</class>
<class value="children2">
<val>children2_value1</val>
<val>children2_value2</val>
</class>
</class>
</root>
...全文
879 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
whatisma 2013-12-27
  • 打赏
  • 举报
回复
引用 3 楼 zbasic 的回复:
特么程序猿都还没来刷论坛。。。
一不小心看到这个问题及下面回复,真心觉的蛋疼。 LZ你能不能把代码写规范点?

var json=...;
function toxml(json);
//这里命名变量冲突,容易出意想不到问题
for(i=0;i<json.length;i++);
//这里的i你知道它是全局变量吗,全局变量意味着对你递归有啥影响?

zbasic 2013-12-24
  • 打赏
  • 举报
回复
引用 16 楼 u012463264 的回复:
你自己保存一下 ,看看能行吗 ?
我贴的是带代码格式的,上面有个复制,拷贝了贴到文本文件就可以了啊. 或者方便加QQ聊下吗?
别闹腰不好 2013-12-24
  • 打赏
  • 举报
回复
你自己保存一下 ,看看能行吗 ?
zbasic 2013-12-24
  • 打赏
  • 举报
回复
引用 14 楼 u012463264 的回复:
[quote=引用 12 楼 zbasic 的回复:] [quote=引用 10 楼 u012463264 的回复:] var s="<root> <"+json.type+" value=\""+json.text+"\">"; $(json.children).each(function(k,v){ $(v).each(function(key,val){ s+="<"+val.type+" value=\""+val.text+"\">"; $(val.children).each(function(ks,vs){ s+=" <"+vs.type+" value=\""+vs.text+"\">"; $(vs.children).each(function(kd,vd){ s+="<val>"+vd.text+"</val>"; }); s+="</"+vs.type+">"; }); s+=" </"+val.type+">"; }); }); s+="</"+json.type+"></root>"; document.write(s); 我写了一个 ,你这json字符串的格式,我根本就保存不了。编码挺SB,没测试。
这个JSON格式是EASYUI tree插件 getData给出来的.因为要用到树形结构的拖拽,只找到这个好用点.JSON格式没法改变. 你这样EACH是固定死的,而树结构有可能嵌套N层,除了递归我实在想不出啥别的办法了.[/quote] 我这也不是固定死的 ,也是遍历的 ,你敢不敢给我个json能保存到文本文档的?[/quote] 谢谢你的帮助. 你敢不敢给我个json能保存到文本文档的? 把下面这段贴到文本文件不就行了?

{
    "type": "class", 
    "text": "children_root", 
    "target": {
        "jQuery17208685037363186575": 114
    }, 
    "checked": false, 
    "state": "open", 
    "children": [
        {
            "type": "class", 
            "text": "children1", 
            "target": {
                "jQuery17208685037363186575": 114
            }, 
            "checked": false, 
            "state": "open", 
            "children": [
                {
                    "type": "class", 
                    "text": "children1_1", 
                    "target": {
                        "jQuery17208685037363186575": 114
                    }, 
                    "checked": false, 
                    "state": "open", 
                    "children": [
                        {
                            "type": "value", 
                            "text": "children1_1_value1"
                        }, 
                        {
                            "type": "value", 
                            "text": "children1_1_value2"
                        }
                    ]
                }
            ]
        }, 
        {
            "type": "class", 
            "text": "children2", 
            "target": {
                "jQuery17208685037363186575": 114
            }, 
            "checked": false, 
            "state": "open", 
            "children": [
                {
                    "type": "value", 
                    "text": "children2_value1"
                }, 
                {
                    "type": "value", 
                    "text": "children2_value2"
                }
            ]
        }
    ]
}
别闹腰不好 2013-12-24
  • 打赏
  • 举报
回复
引用 12 楼 zbasic 的回复:
[quote=引用 10 楼 u012463264 的回复:] var s="<root> <"+json.type+" value=\""+json.text+"\">"; $(json.children).each(function(k,v){ $(v).each(function(key,val){ s+="<"+val.type+" value=\""+val.text+"\">"; $(val.children).each(function(ks,vs){ s+=" <"+vs.type+" value=\""+vs.text+"\">"; $(vs.children).each(function(kd,vd){ s+="<val>"+vd.text+"</val>"; }); s+="</"+vs.type+">"; }); s+=" </"+val.type+">"; }); }); s+="</"+json.type+"></root>"; document.write(s); 我写了一个 ,你这json字符串的格式,我根本就保存不了。编码挺SB,没测试。
这个JSON格式是EASYUI tree插件 getData给出来的.因为要用到树形结构的拖拽,只找到这个好用点.JSON格式没法改变. 你这样EACH是固定死的,而树结构有可能嵌套N层,除了递归我实在想不出啥别的办法了.[/quote] 我这也不是固定死的 ,也是遍历的 ,你敢不敢给我个json能保存到文本文档的?
zbasic 2013-12-24
  • 打赏
  • 举报
回复
引用 9 楼 gaofuqi 的回复:
xml和Json的相互转换,下面有你想要的答案。 http://bbs.csdn.net/topics/300052586
这个帖子是值得研究下,但因为我上面给出的JSON结构是精简过的,本身JSON中有些字段是不需要的,如果直接转的话就会出现很多无用的字段.现在项目紧,得先把问题解决....
zbasic 2013-12-24
  • 打赏
  • 举报
回复
引用 10 楼 u012463264 的回复:
var s="<root> <"+json.type+" value=\""+json.text+"\">"; $(json.children).each(function(k,v){ $(v).each(function(key,val){ s+="<"+val.type+" value=\""+val.text+"\">"; $(val.children).each(function(ks,vs){ s+=" <"+vs.type+" value=\""+vs.text+"\">"; $(vs.children).each(function(kd,vd){ s+="<val>"+vd.text+"</val>"; }); s+="</"+vs.type+">"; }); s+=" </"+val.type+">"; }); }); s+="</"+json.type+"></root>"; document.write(s); 我写了一个 ,你这json字符串的格式,我根本就保存不了。编码挺SB,没测试。
这个JSON格式是EASYUI tree插件 getData给出来的.因为要用到树形结构的拖拽,只找到这个好用点.JSON格式没法改变. 你这样EACH是固定死的,而树结构有可能嵌套N层,除了递归我实在想不出啥别的办法了.
zbasic 2013-12-24
  • 打赏
  • 举报
回复
引用 8 楼 u012463264 的回复:
[quote=引用 7 楼 u012463264 的回复:] [quote=引用 6 楼 zbasic 的回复:] [quote=引用 5 楼 u012463264 的回复:] 直接遍历json不是更简单吗
怎么个遍历法,请指教[/quote] $.each()[/quote]
引用 7 楼 u012463264 的回复:
[quote=引用 6 楼 zbasic 的回复:] [quote=引用 5 楼 u012463264 的回复:] 直接遍历json不是更简单吗
怎么个遍历法,请指教[/quote] $.each()[/quote]要是没解决我给你写个?[/quote] 谢谢提醒,实际用$.each()测试了下.因为上面给出的JSON结构是我简化过的.实际每个节点上还有很多其他数据.用EACH遍历的时候效率很低.我得出的这个JSON是 EASY UI tree 返回的整个树结构.一个树节点有可能达到上千,所以效率也是考虑的重要因数. 这里给出没有精简其他字段的一个JSON

var json={
	"type":"class",
    "text": "children_root",
	"target": {
        "jQuery17208685037363186575": 114
    }, 
    "checked": false, 
    "state": "open", 
    "children": [
        {
			"type":"class",
            "text": "children1",
			"target": {
				"jQuery17208685037363186575": 114
			}, 
			"checked": false, 
			"state": "open", 
            "children": [
                {
					"type":"class",
                    "text": "children1_1",
					"target": {
						"jQuery17208685037363186575": 114
					}, 
					"checked": false, 
					"state": "open", 
                    "children": [
                        {
							"type":"value",
                            "text": "children1_1_value1"
                        },
                        {
							"type":"value",
                            "text": "children1_1_value2"
                        }
                    ]
                }
            ]
        },
        {
			"type":"class",
            "text": "children2",
			"target": {
				"jQuery17208685037363186575": 114
			}, 
			"checked": false, 
			"state": "open", 
            "children": [
                {
					"type":"value",
                    "text": "children2_value1"
                },
                {
					"type":"value",
                    "text": "children2_value2"
                }
            ]
        }
    ]
};


var xml="<root>";
xml += toxml(json);
xml += "</root>";


	
	function toxml(json){
		var xml_str="";
		alert(json.length);
			$.each(json,function(key,value){
					if(key=="text"){
						 xml_str += "<class value='"+value+"'>";
						 xml_str += toxml(json.children);
						 xml_str += "</class>";
					}
				});
			return xml_str;	
		}

尝试了一下写,还是得不出要的结构. 我想要得到的结果是 <root> <class value="children_root"> <class value="children1"> <class value="children1_1"> <val>children1_1_value1</val> <val>children1_1_value2</val> </class> </class> <class value="children2"> <val>children2_value1</val> <val>children2_value2</val> </class> </class> </root> 麻烦看能不能用递归的方法得出这个结果.谢谢.
别闹腰不好 2013-12-24
  • 打赏
  • 举报
回复
var s="<root> <"+json.type+" value=\""+json.text+"\">"; $(json.children).each(function(k,v){ $(v).each(function(key,val){ s+="<"+val.type+" value=\""+val.text+"\">"; $(val.children).each(function(ks,vs){ s+=" <"+vs.type+" value=\""+vs.text+"\">"; $(vs.children).each(function(kd,vd){ s+="<val>"+vd.text+"</val>"; }); s+="</"+vs.type+">"; }); s+=" </"+val.type+">"; }); }); s+="</"+json.type+"></root>"; document.write(s); 我写了一个 ,你这json字符串的格式,我根本就保存不了。编码挺SB,没测试。
gaofuqi 2013-12-24
  • 打赏
  • 举报
回复
xml和Json的相互转换,下面有你想要的答案。 http://bbs.csdn.net/topics/300052586
别闹腰不好 2013-12-24
  • 打赏
  • 举报
回复
引用 7 楼 u012463264 的回复:
[quote=引用 6 楼 zbasic 的回复:] [quote=引用 5 楼 u012463264 的回复:] 直接遍历json不是更简单吗
怎么个遍历法,请指教[/quote] $.each()[/quote]
引用 7 楼 u012463264 的回复:
[quote=引用 6 楼 zbasic 的回复:] [quote=引用 5 楼 u012463264 的回复:] 直接遍历json不是更简单吗
怎么个遍历法,请指教[/quote] $.each()[/quote]要是没解决我给你写个?
别闹腰不好 2013-12-24
  • 打赏
  • 举报
回复
引用 6 楼 zbasic 的回复:
[quote=引用 5 楼 u012463264 的回复:] 直接遍历json不是更简单吗
怎么个遍历法,请指教[/quote] $.each()
zbasic 2013-12-24
  • 打赏
  • 举报
回复
引用 5 楼 u012463264 的回复:
直接遍历json不是更简单吗
怎么个遍历法,请指教
别闹腰不好 2013-12-24
  • 打赏
  • 举报
回复
直接遍历json不是更简单吗
zbasic 2013-12-24
  • 打赏
  • 举报
回复
引用 1 楼 defonds 的回复:
children2 节点和 1 分开,自己调用 toxml
这里只是个示范结构,children2下也有可能有2个或者多个class的并行结构。
zbasic 2013-12-24
  • 打赏
  • 举报
回复
特么程序猿都还没来刷论坛。。。
zbasic 2013-12-24
  • 打赏
  • 举报
回复
特么程序猿都还没来刷论坛。。。
Defonds 2013-12-24
  • 打赏
  • 举报
回复
children2 节点和 1 分开,自己调用 toxml
zbasic 2013-12-24
  • 打赏
  • 举报
回复
引用 21 楼 u012463264 的回复:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script language="javascript" type="text/javascript" src="js/jquery-1.4.1.js"></script> <script type="text/javascript"> var json={"type":"class","text":"children_root","target":{"jQuery17208685037363186575":114},"checked":false,"state":"open","children":[{"type":"class","text":"children1","target":{"jQuery17208685037363186575":114},"checked":false,"state":"open","children":[{"type":"class","text":"children1_1","target":{"jQuery17208685037363186575":114},"checked":false,"state":"open","children":[{"type":"value","text":"children1_1_value1"},{"type":"value","text":"children1_1_value2"}]}]},{"type":"class","text":"children2","target":{"jQuery17208685037363186575":114},"checked":false,"state":"open","children":[{"type":"value","text":"children2_value1"},{"type":"value","text":"children2_value2"}]}]}; var s="<root> <"+json.type+" value=\""+json.text+"\">"; s+=jsonList(json.children); s+="</"+json.type+"></root>"; alert(s); //这个方法只接受children节点, function jsonList(json){ var s=""; $(json).each(function(k,v){ s+="<"+v.type+" value=\""+v.text+"\">"; $(v.children).each(function(key,val){ if(val.children!=null&&val.children.length>0){ s+=jsonList(val); }else{ s+="<val>"+val.text+"</val>"; } }); s+=" </"+v.type+">"; }); return s; } </script> </head> <body> </body> </html> 我给你写了个 ,递归 可以用的 试试
非常感谢.这样写可以用.搞定了...
别闹腰不好 2013-12-24
  • 打赏
  • 举报
回复
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script language="javascript" type="text/javascript" src="js/jquery-1.4.1.js"></script> <script type="text/javascript"> var json={"type":"class","text":"children_root","target":{"jQuery17208685037363186575":114},"checked":false,"state":"open","children":[{"type":"class","text":"children1","target":{"jQuery17208685037363186575":114},"checked":false,"state":"open","children":[{"type":"class","text":"children1_1","target":{"jQuery17208685037363186575":114},"checked":false,"state":"open","children":[{"type":"value","text":"children1_1_value1"},{"type":"value","text":"children1_1_value2"}]}]},{"type":"class","text":"children2","target":{"jQuery17208685037363186575":114},"checked":false,"state":"open","children":[{"type":"value","text":"children2_value1"},{"type":"value","text":"children2_value2"}]}]}; function writed(json){ var s="<root> <"+json.type+" value=\""+json.text+"\">"; if(json.children!=null) s+=jsonList(json.children); s+="</"+json.type+"></root>"; return s; } alert(writed(json)); //这个方法只接受children节点, function jsonList(json){ var s=""; $(json).each(function(k,v){ s+="<"+v.type+" value=\""+v.text+"\">"; $(v.children).each(function(key,val){ if(val.children!=null&&val.children.length>0){ s+=jsonList(val); }else{ s+="<val>"+val.text+"</val>"; } }); s+=" </"+v.type+">"; }); return s; } </script> </head> <body> </body> </html>
加载更多回复(5)

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧