我正在构建一个小型应用程序,一切都很好,直到必须打印作为键参数的对象(例如,对于地址,我有一个对象作为参数,并带有诸如街道名称之类的子键,门牌号码等)。我也应该如何打印该对象?
这是javascript:
function DropdownFunction(elem) {
document.getElementById("Div"+elem.id).classList.toggle("show");
}
function DeleteBtnFunction(elem) {
}
function DeleteBtnFunction(elem) {
}
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var objects = JSON.parse(this.responseText);
for(var i=0;i<objects.length;i++){
var createLiElement = document.createElement("LI");
createLiElement.setAttribute("id", "listElement"+i);
var usersName = document.createTextNode(objects[i].name);
createLiElement.appendChild(usersName);
document.getElementById("userList").appendChild(createLiElement);
userList.innerHTML += "<br>";
hiddenInfo = document.createElement('div');
hiddenInfo.setAttribute("id", "DivBtn"+i);
hiddenInfo.setAttribute("class", "HiddenInfos");
document.getElementById("userList").appendChild(hiddenInfo);
var keyName;
for (keyName in objects[i]) {
var createPfield = document.createElement("p");
var showKeyValue = document.createTextNode(keyName + ": " + objects[i][keyName]);
createPfield.appendChild(showKeyValue);
document.getElementById("DivBtn"+i).appendChild(createPfield);
和users.txt看起来像这样:
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
},
"phone": "1-770-736-8031 x56442",
"website": "hildegard.org",
"company": {
"name": "Romaguera-Crona",
"catchPhrase": "Multi-layered client-server neural-net",
"bs": "harness real-time e-markets"
}
以下是代码的行为方式: enter image description here