我尝试使用JSON数据创建表,我不知道如何使用AJAX调用获取嵌套节点数据?
{
"id": 1,
"label": "A",
"child1": [
{
"id": 2,
"label": "B",
"child2.1": [
{
"id": 5,
"label": "B"
},
{
"id": 6,
"label": "B"
},
{
"id": 7,
"label": "B"
}
]
}
],
"child2": [
{
"id": 3,
"label": "C"
}
],
"child3": [
{
"id": 4,
"label": "D",
"child4.1": [
{
"id": 8,
"label": "B"
},
{
"id": 9,
"label": "B"
}
]
}
]
}
如何将此数据提取到表中。我尝试过这种方式,但却发生错误。
alert();
var xmlhttp = new XMLHttpRequest();
var url = "{
"id": 1,
"label": "A",
"child1": [{
"id": 2,"label": "B",
"child2.1": [{
"id": 5,
"label": "B"
},
{
"id": 6,
"label": "B"
},
{
"id": 7,
"label": "B"
}
]
}
],
"child2": [
{
"id": 3,
"label": "C"
}
],
"child3": [
{
"id": 4,
"label": "D",
"child4.1": [
{
"id": 8,
"label": "B"
},
{
"id": 9,
"label": "B"
}
]
}
]
}";
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
myFunction(xmlhttp.responseText);
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
function myFunction(response) {
var arr = JSON.parse(response);
var i;
var out = "<table>";
var z = arr.length;
for(i = 0; i < arr.length; i++) {
out += "<tr><td>" +
arr[i].id +
alert(i);
"</td><td>" +
arr[i].City +
"</td><td>" +
arr[i].Country +
"</td></tr>";
}
out += "</table>";
document.getElementById("id01").innerHTML = out;
}
我甚至尝试使用alert();
进行调试,但它无效。警报不起作用,我不知道,错误在哪里。