0
[{"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...

在此处输入图像描述

4

1 回答 1

3

你可能想要更接近的东西

$.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);
            }
        });
    }
});

请注意,上面没有打印父母的标签,您可能需要添加它。

于 2013-03-13T05:19:38.987 回答