[{"tag":35,"value":"W","children":[
{"tag":55,"value":"GOOG","children":null},
{"tag":262,"value":"ghost332002m0","children":null},
{"tag":268,"value":"1","children":[
{"tag":269,"value":"B","children":null},
{"tag":271,"value":"0","children":null},
{"tag":336,"value":"3","children":null}
]}
]},
{"tag":35,"value":"W","children":[
{"tag":55,"value":"GOOG","children":null},
{"tag":262,"value":"ghost332002m0","children":null},
{"tag":268,"value":"0","children":null}
]}
]
这是 JSON,它是 FIX 市场数据,并且您有这些嵌套组,因此这是我在 JSON 中对这些 FIX 消息的表示。无论如何,我将其发送到我的网络客户端,并需要将其展平回到显示器中。
$.getJSON('/receive', function(data, returnValue) {
$.each(data, function(index,value) {
$('#output').append('<p>');
appendStuff(value);
$('#output').append('</p>');
});
function appendStuff(children) {
debug_var.push(children);
$.each(children, function(child) {
$('#output').append(child.tag+'='+child.value+' ');
if (child.children != null) {
appendStuff(child.children);
}
})
}
我正在尝试使用递归来旋转这些数据并打印出所有内容。我得到的是:
undefined=undefined undefined=undefined undefined=undefined
我做错了什么?
哦,数据都在debug_var中...
最佳答案
您可能想要更接近的东西
$.getJSON('/receive', function (data) {
$.each(data, function(index,value) {
$('#output').append('<p>');
appendStuff(value.children || []);
$('#output').append('</p>');
});
function appendStuff(children) {
$.each(children, function(i, child) {
$('#output').append(child.tag+'='+child.value+' ');
if (child.children != null) {
appendStuff(child.children);
}
});
}
});
请注意,上面没有打印 parent 的标签,您可能需要添加它。
关于javascript - 如何使用 jquery 递归地附加此树数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15377283/