使用flare.json文件在HTML中构造嵌套的无序列表?

时间:2014-04-23 00:17:39

标签: javascript html json

如何在HTML中的嵌套无序列表中构建以下flare.json文件?我打算使用嵌套的无序列表来显示图表。

有人可以指导我如何转换吗?

{
    "name": "flare",
    "children": [
        {
            "name": "analytics",
            "children": [
                {
                    "name": "cluster",
                    "children": [
                        {"name": "AgglomerativeCluster", "size": 3938},
                        {"name": "CommunityStructure", "size": 3812},
                        {"name": "HierarchicalCluster", "size": 6714},
                        {"name": "MergeEdge", "size": 743}
                    ]
                },
                {
                    "name": "graph",
                    "children": [
                        {"name": "BetweennessCentrality", "size": 3534},
                        {"name": "LinkDistance", "size": 5731},
                        {"name": "MaxFlowMinCut", "size": 7840},
                        {"name": "ShortestPaths", "size": 5914},
                        {"name": "SpanningTree", "size": 3416}
                    ]
                },
                {
                    "name": "optimization",
                    "children": [
                        {"name": "AspectRatioBanker", "size": 7074}
                    ]
                }
            ]
        }
    ]
}

1 个答案:

答案 0 :(得分:0)

你的问题有些含糊不清,但这是我的抨击。如果不是您正在寻找的话,请告诉我。

以下是相关代码:

function pprint(o) {
  if(typeof o != 'object') {
    return o.toString(); /* Note: this check won't work for javascript in general, but should be okay for the result of JSON.parse */
  }
  var open = '<ul>', close = '</ul>', html = '', notArray = true;
  if(Array.isArray(o)) {
    open = '<ol>';
    close = '</ol>';
    notArray = false;
  }
  $.each(o, function(k,v){
    html += '<li>';
    if(notArray) {
      html += k+': ';
    }
    html += pprint(v)+'</li>';
  });
  return open+html+close;
}

在这里演示:http://codepen.io/devm33/pen/lspjh/