将每个JSON对象及其项目放入<li>,具体取决于有多少对象

时间:2017-02-21 15:33:53

标签: javascript html json

JSON(我有3个对象):

[{"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");

    },
});

1 个答案:

答案 0 :(得分:0)

尝试在成功功能中使用Array.forEach

&#13;
&#13;
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);
};
 
});
&#13;
&#13;
&#13;