Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/431.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在JSON上迭代时出现问题_Javascript_Jquery_Json - Fatal编程技术网

Javascript 在JSON上迭代时出现问题

Javascript 在JSON上迭代时出现问题,javascript,jquery,json,Javascript,Jquery,Json,我有JSON: { "GetCommentsByPostResult": [ { "CommentCreated": "\\/Date(1305736030505+0100)\\/", "CommentText": "Comment 1" }, { "CommentCreated": "\\/Date(1305736030505+0100)\\/",

我有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个带有“undefined”的警报屏幕……不知道为什么会有人知道?

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

这应该起作用:

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

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

这应该起作用:

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

惊人,本应更仔细地阅读文档,但耶,好,仍在学习:-)将勾选为答案惊人,本应更仔细地阅读文档,但耶,好,仍在学习:-)将勾选为答案