此代码将在HTML表格打印JSON阵列。 目录文件file.json的:
[[ 50.65, 5.37 , 4358684.11 ],
[ 50.68, 5.86 , 1849705.79 ],
[ 50.33, 6.84 , 961107.46 ],
[ 50.55, 7.32 , 1587473.93 ]]
HTML/JavaScript的
var xmlhttp = new XMLHttpRequest();
var url = "file.json";
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var myArr = JSON.parse(xmlhttp.responseText);
myFunction(myArr);
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
function myFunction(arr) {
var out = "
var i;
for(i = 0; i < arr.length; i++) {
out += "
";for (j = 0; j < 3; j++) {
out += "
"+arr[i][j]+"";}
out += "
";}
out += "
";document.getElementById("JSONtable").innerHTML = out;
}