JSON(我有 3 个 a 对象):
[{"NotificationID":13,
"SourceID":1,
"NotificationGroup":null,
"NotificationGroupID":0,
"NotificationName":"Updates",
"Description":"Server Maintenance",
"NotificationDate":"2017-01-01T00:00:00",
"DueDate":"2017-01-01T00:00:00",
"NotificationTo":"Me",
"ReadFlag":0,
"IsDelete":0,
"CreateDateTime":"2017-01-01T00:00:00",
"CreateBy":"Me",
"ModifyDateTime":"2017-01-01T00:00:00",
"ModifyBy":"Me",
"NotificationType":1,
"SourceName":"test"}]
OUTPUT 应显示对象以及对象的项目列表
我的 JavaScript:注意 ul 标签和 li 标签尚未创建
var notif = $.ajax({
url: serviceApi,
dataType: "json",
success: function (data) {
var notifObj = JSON.parse(JSON.stringify(data));
var divElement = document.createElement("ul");
var divElement_2 = document.createElement("li");
},
});
最佳答案
尝试在 success 函数中使用Array.forEach
。
object = [{"NotificationID":13,
"SourceID":1,
"NotificationGroup":null,
"NotificationGroupID":0,
"NotificationName":"Updates",
"Description":"Server Maintenance",
"NotificationDate":"2017-01-01T00:00:00",
"DueDate":"2017-01-01T00:00:00",
"NotificationTo":"Me",
"ReadFlag":0,
"IsDelete":0,
"CreateDateTime":"2017-01-01T00:00:00",
"CreateBy":"Me",
"ModifyDateTime":"2017-01-01T00:00:00",
"ModifyBy":"Me",
"NotificationType":1,
"SourceName":"test"}];
object.forEach(function(obj){
var divElement = document.createElement("ul");
document.body.appendChild(divElement);
for(x in obj){
var divElement_2 = document.createElement("li");
divElement_2.textContent = obj[x];
divElement.appendChild(divElement_2);
};
});
关于javascript - 根据对象的数量将每个 JSON 对象及其项目放入 <li> 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42371578/