繁体   English   中英

遍历JSON的问题

[英]Problem iterating over JSON

我有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回调中的第一个参数(在数组上调用时)是数组的索引。

这应该工作:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM