使用Javascript将JSON嵌套到编号的HTML表

时间:2022-09-15 12:59:06

I'm trying to generate HTML table from JSON

我正在尝试从JSON生成HTML表

The JSON provided is deeply nested. With the help of this thread How do I loop through deeply nested properties of a JavaScript object?, I am able to get the values of the JSON but I am confused on how to generate the HTML table

所提供的JSON深度嵌套。在这个线程的帮助下,我如何对JavaScript对象的深层嵌套属性进行循环?,我可以获得JSON的值,但是我不知道如何生成HTML表

var districts = {
    "district": [{
        "ration": 4,
        "states": [{
            "name": "Louisiana",
            "population": 42383,
            "cities": [{
                "name": "Cavalero"
            }]
        }]
    }, {
        "ration": 1,
        "states": [{
            "name": "Colorado",
            "population": 980,
            "cities": []
        }, {
            "name": "Arkansas",
            "population": 58998,
            "cities": []
        }, {
            "name": "Illinois",
            "population": 59333,
            "cities": [{
                "name": "Kenwood"
            }]
        }]
    }, {
        "ration": 2,
        "states": [{
            "name": "Washington",
            "population": 83984,
            "cities": [{
                "name": "Conestoga"
            }, {
                "name": "Whitehaven"
            }, {
                "name": "Dellview"
            }]
        }, {
            "name": "West Virginia",
            "population": 38034,
            "cities": []
        }]
    }]
};
var i, district, j, states, k, cities;
for (i = 0; i < districts.district.length; i++) {
    district = districts.district[i];
    print(i + 1, ". District", i + 1, "consists of following states", "--- ration", district.ration);
    for (j = 0; j < district.states.length; j++) {
        states = district.states[j];
        var said = (states.cities.length > 0) ? ("consists of following cities") : ("");
        print(i + 1, ".", j + 1, states.name, said, "--- population", states.population);
        for (k = 0; k < states.cities.length; k++) {
            cities = states.cities[k];
            print("     ", i + 1, ".", j + 1, ".", k + 1, cities.name);
        }
    }
}

Run this on Ideone

上运行这个Ideone

Any pointers/help/suggestions appreciated

任何指针/帮助/建议表示赞赏

2 个解决方案

#1


1  

You will need to generate a table, like this:

您将需要生成一个表,如下所示:

var districts = {
    "district": [{
        "ration": 4,
        "states": [{
            "name": "Louisiana",
            "population": 42383,
            "cities": [{
                "name": "Cavalero"
            }]
        }]
    }, {
        "ration": 1,
        "states": [{
            "name": "Colorado",
            "population": 980,
            "cities": []
        }, {
            "name": "Arkansas",
            "population": 58998,
            "cities": []
        }, {
            "name": "Illinois",
            "population": 59333,
            "cities": [{
                "name": "Kenwood"
            }]
        }]
    }, {
        "ration": 2,
        "states": [{
            "name": "Washington",
            "population": 83984,
            "cities": [{
                "name": "Conestoga"
            }, {
                "name": "Whitehaven"
            }, {
                "name": "Dellview"
            }]
        }, {
            "name": "West Virginia",
            "population": 38034,
            "cities": []
        }]
    }]
};
//Start of the table, including header
var table = '<table><thead><tr><th>Num</th><th>District</th><th>Population</th><th>Ration</th></tr></thead><tbody>';

//Num
for (var i = 0; i < districts.district.length; i++) {
    //District
    var district = districts.district[i];
    //First row
    table += '<tr><td>' + (i + 1) + '</td><td>District ' + district.ration + ' consists of the following states:</td><td></td><td>' + district.ration + '</td></tr>';
    //States
    var states = district.states;
    for (var j = 0; j < states.length; j++) {
        table += '<tr><td></td><td>' + (i + 1) + '.' + (j + 1) + ' ' + states[j] + ((states[j].cities && states[j].cities.length) ? ' consists of following cities:' : '') + '</td><td>' + states[j].population + '</td><td></td></tr>';
        //Cities
        if (states[j].cities) {
            for (var k = 0; k < states[j].cities; k++) {
                table += '<tr><td></td><td>' + (i + 1) + '.' + (j + 1) + '.' + (k + 1) + ' ' + states[j].cities[k].name + '</td><td></td><td></td></tr>';
            }
        }
    }
}

//End of the table
table += '</tbody></table>';

and then add table somewhere into your html.

然后将表添加到html中。

#2


0  

if you want to generate the desired output as on that link, you can use <ol> <li></li> <li> <ol><li></li></ol> <li> </ol> instead of table. generate it using javascript. you can use below code to generate your ordered list which should be inserted on the main ordered list tag.

如果您想要在该链接上生成所需的输出,可以使用

  1. < li>< ol>
而不是表。使用javascript生成它。您可以使用下面的代码生成您的有序列表,该列表应该被插入到主命令列表标记中。

var district = districts.district;

function generateCities(cities){
    cities.map((city) => {
        return (
            "<li>"+ city.name  + "</li>"
        )
    })
}

function generateStates(states, generateCities){
    states.map((stat) => {
        return (
            "<li>"+stat.name + " consists of following cities --- population " + stat.population + "</li>"
            +"<ol>" + generateCities(stat.cities) + "</ol>"
            )
    });
}
function generateMyHtml(district, generateStates){
    district.map((dist, index) => {
        return (
            "<li> District "+ index + "consists of following states --- ration " + dist.ration + "</li>"
             +"<ol>" + generateStates(dist.states) + "</ol>"
        )   
    });
};

hope this is helpful

希望这是有帮助的

#1


1  

You will need to generate a table, like this:

您将需要生成一个表,如下所示:

var districts = {
    "district": [{
        "ration": 4,
        "states": [{
            "name": "Louisiana",
            "population": 42383,
            "cities": [{
                "name": "Cavalero"
            }]
        }]
    }, {
        "ration": 1,
        "states": [{
            "name": "Colorado",
            "population": 980,
            "cities": []
        }, {
            "name": "Arkansas",
            "population": 58998,
            "cities": []
        }, {
            "name": "Illinois",
            "population": 59333,
            "cities": [{
                "name": "Kenwood"
            }]
        }]
    }, {
        "ration": 2,
        "states": [{
            "name": "Washington",
            "population": 83984,
            "cities": [{
                "name": "Conestoga"
            }, {
                "name": "Whitehaven"
            }, {
                "name": "Dellview"
            }]
        }, {
            "name": "West Virginia",
            "population": 38034,
            "cities": []
        }]
    }]
};
//Start of the table, including header
var table = '<table><thead><tr><th>Num</th><th>District</th><th>Population</th><th>Ration</th></tr></thead><tbody>';

//Num
for (var i = 0; i < districts.district.length; i++) {
    //District
    var district = districts.district[i];
    //First row
    table += '<tr><td>' + (i + 1) + '</td><td>District ' + district.ration + ' consists of the following states:</td><td></td><td>' + district.ration + '</td></tr>';
    //States
    var states = district.states;
    for (var j = 0; j < states.length; j++) {
        table += '<tr><td></td><td>' + (i + 1) + '.' + (j + 1) + ' ' + states[j] + ((states[j].cities && states[j].cities.length) ? ' consists of following cities:' : '') + '</td><td>' + states[j].population + '</td><td></td></tr>';
        //Cities
        if (states[j].cities) {
            for (var k = 0; k < states[j].cities; k++) {
                table += '<tr><td></td><td>' + (i + 1) + '.' + (j + 1) + '.' + (k + 1) + ' ' + states[j].cities[k].name + '</td><td></td><td></td></tr>';
            }
        }
    }
}

//End of the table
table += '</tbody></table>';

and then add table somewhere into your html.

然后将表添加到html中。

#2


0  

if you want to generate the desired output as on that link, you can use <ol> <li></li> <li> <ol><li></li></ol> <li> </ol> instead of table. generate it using javascript. you can use below code to generate your ordered list which should be inserted on the main ordered list tag.

如果您想要在该链接上生成所需的输出,可以使用

  1. < li>< ol>
而不是表。使用javascript生成它。您可以使用下面的代码生成您的有序列表,该列表应该被插入到主命令列表标记中。

var district = districts.district;

function generateCities(cities){
    cities.map((city) => {
        return (
            "<li>"+ city.name  + "</li>"
        )
    })
}

function generateStates(states, generateCities){
    states.map((stat) => {
        return (
            "<li>"+stat.name + " consists of following cities --- population " + stat.population + "</li>"
            +"<ol>" + generateCities(stat.cities) + "</ol>"
            )
    });
}
function generateMyHtml(district, generateStates){
    district.map((dist, index) => {
        return (
            "<li> District "+ index + "consists of following states --- ration " + dist.ration + "</li>"
             +"<ol>" + generateStates(dist.states) + "</ol>"
        )   
    });
};

hope this is helpful

希望这是有帮助的