将JSON文件转换为嵌套列表

时间:2016-02-17 10:27:51

标签: html json list handlebars.js mustache

我有以下 JSON 文件:

    {
    "teams": {
        "bayernMunichFC": {
            "country": "Germany",
            "city": "Munich",
            "name": "Bayern Munich"
        },
        "1860Munich": {
            "country": "Germany",
            "city": "Munich",
            "name": "1860 Munich"
        },
        "borussiaDortmundFC": {
            "country": "Germany",
            "city": "Dortmund",
            "name": "Borussia Dortmund"
        },
        "hibernianFC": {
            "country": "Scotland",
            "city": "Edinburgh",
            "name": "Hibernian FC"
        },
        "heartOfMidlothianFC": {
            "country": "Scotland",
            "city": "Edinburgh",
            "name": "Heart of Midlothian FC"
        },
        "celticFC": {
            "country": "Scotland",
            "city": "Glasgow",
            "name": "Celtic FC"
        },
        "rangersFC": {
            "country": "Scotland",
            "city": "Glasgow",
            "name": "Rangers FC"
        }
    }
}

我想要做的是在 HTML 中使用嵌套列表,最好使用Mustache,它将在浏览器中输出:

- Germany
   - Munich
     - Bayern Munich
     - 1860 Munich
   - Dortmund
     - Borussia Dortmund
 - Scotland
   - Edinburgh
     - Hibernian FC
     - Heart of Midlothian FC
   - Glasgow
     - Celtic FC
     - Rangers FC

我努力将团队组织成所需的结构。是否可以使用以这种方式格式化的 JSON 执行此操作,还是需要进行一些数据操作?如果是的话,你能给我任何帮助或建议吗?

非常感谢!

P.S。密钥例如" bayernMunichFC"," heartOfMidlothianFC"等等;将用作dataRef标记,因为每个团队名称将是一个将dataRef属性传递给URL的链接。

1 个答案:

答案 0 :(得分:0)

好吧,这很有趣,我不知道它是否有帮助,但我写了一些代码

var results {};
var data= YOUR_JSON;

for(var key in data.teams){  

    if(data.teams.hasOwnProperty(key)) {
        if(!(results[data.teams[key].country] instanceof Object)){
            results[data.teams[key].country] = {};
            results[data.teams[key].country][data.teams[key].city] = {};
            results[data.teams[key].country][data.teams[key].city][key] = data.teams[key].name;    
        }else {
            if(!(results[data.teams[key].country][data.teams[key].city] instanceof Object)){
                results[data.teams[key].country][data.teams[key].city] = {};
                results[data.teams[key].country][data.teams[key].city][key] = data.teams[key].name;  
            }else{
                results[data.teams[key].country][data.teams[key].city][key] = data.teams[key].name;  
            }
        }
    }  
}

的console.log(结果)

将你的json转换为这个输出:

Germany: Object
  Dortmund: Array[1]
    0: "Borussia Dortmund"
  Munich: Array[2]
    0: "Bayern Munich"
    1: "1860 Munich"

Scotland: Object
  Edinburgh: Array[2]
    0: "Hibernian FC"
    1: "Heart of Midlothian FC"

  Glasgow: Array[2]
    0: "Celtic FC"
    1: "Rangers FC"

不是很好,它是你的预期订单,不能与这里有太大的不同

如果你想生成一个列表,你可以这样做:

var html  = "<ul>";

for(var key in results){
    html += "<li>"+key+"</li><ul>";
    for(var akey in results[key]){
        html += "<li>"+akey+"</li><ul>";
        for(var bkey in results[key][akey]){
            html += "<li><a href='"+bkey+"'>"+results[key][akey][bkey]+"</a></li>";
        }
        html += "</ul>";
    }
    html += "</ul>";
}

html += "</ul>"

输出你想要的输出:)玩得开心

<ul><li>Germany</li><ul><li>Munich</li><ul><li><a href='bayernMunichFC'>Bayern Munich</a></li><li><a href='1860Munich'>1860 Munich</a></li></ul><li>Dortmund</li><ul><li><a href='borussiaDortmundFC'>Borussia Dortmund</a></li></ul></ul><li>Scotland</li><ul><li>Edinburgh</li><ul><li><a href='hibernianFC'>Hibernian FC</a></li><li><a href='heartOfMidlothianFC'>Heart of Midlothian FC</a></li></ul><li>Glasgow</li><ul><li><a href='celticFC'>Celtic FC</a></li><li><a href='rangersFC'>Rangers FC</a></li></ul></ul></ul>