开发者

Problem iterating over JSON

开发者 https://www.devze.com 2023-03-07 10:57 出处:网络
I have the JSON: { \"GetCommentsByPostResult\": [ { \"CommentCreated\": \"\\\\/Date(1305736030505+0100)\\\\/\",

I have the 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?


Because the first parameter in $.each's callback (when called on an array) is the index in to the array.

This should work:

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

精彩评论

暂无评论...
验证码 换一张
取 消