简体   繁体   中英

Construct nested unordered list in HTML using flare.json file?

How do I construct the below flare.json file to a nested unordered list in HTML? I am planning to use the nested unordered list to display charts.

Can someone guide me on how to convert?

{
    "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}
                    ]
                }
            ]
        }
    ]
}

There's some ambiguity in your question, but here's my stab at it. Let me know if it's not what you're looking for.

Here's the relevant code:

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;
}

Demo here: http://codepen.io/devm33/pen/lspjh/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM