简体   繁体   English

遍历JSON的问题

[英]Problem iterating over JSON

I have the JSON: 我有JSON:

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

And Im trying to iterate over it using this: 我正在尝试使用以下方法对其进行迭代:

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

But all im getting is 3 alert screens with 'undefined' in it....no idea why anyone know? 但是,所有即时消息都是3个带有“ undefined”的警报屏幕。...不知道为什么有人知道吗?

Because the first parameter in $.each 's callback (when called on an array) is the index in to the array. 因为$.each回调中的第一个参数(在数组上调用时)是数组的索引。

This should work: 这应该工作:

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

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

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