81,091
社区成员
发帖
与我相关
我的任务
分享
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);
var json=...;
function toxml(json);
//这里命名变量冲突,容易出意想不到问题
for(i=0;i<json.length;i++);
//这里的i你知道它是全局变量吗,全局变量意味着对你递归有啥影响?
{
"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 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>
麻烦看能不能用递归的方法得出这个结果.谢谢.