迭代 JSON 时出现问题

Problem iterating over JSON

本文关键字:问题 JSON 迭代      更新时间:2023-09-26

我有JSON:

{
    "GetCommentsByPostResult": [
        {
            "CommentCreated": "''/Date(1305736030505+0100)''/",
            "CommentText": "Comment 1"
        },
        {
            "CommentCreated": "''/Date(1305736030505+0100)''/",
            "CommentText": "Comment 2"
        },
        {
            "CommentCreated": "''/Date(1305736030505+0100)''/",
            "CommentText": "Comment 2"
        }
    ]
}

我试图使用这个来迭代它:

$.each(data.GetCommentsByPostResult, function (e) {
                        alert(e.CommentText);
                    });

但是我得到的只是 3 个带有"未定义"的警报屏幕......不知道为什么有人知道?

因为$.each回调中的第一个参数(在数组上调用时)是数组中的索引。

这应该有效:

$.each(data.GetCommentsByPostResult, function(index, element) {
    alert(element.CommentText);
});