最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery Accessing a JSON object? - Stack Overflow

programmeradmin3浏览0评论

I get the following back from an $.ajax({ POST.....

[{"total_votes":1,"options":[{"id":40,"vote_count":0,"users":[]},{"id":41,"vote_count":1,"users":[{"photo":"xxxxxxxxxxx.png","name":"XXXXX,"id":1}]},{"id":42,"vote_count":0,"users":[]}]}]

so I try to get total_votes with:

    success: function(e) {
        console.log(e['total_votes'])       
    }

also try to get

        console.log( e['options'].length() )
        console.log( e['options'][0]['id'] )

Suggestions on why I keep getting undefined? Also is there a better way to loop through options?

Thanks

I get the following back from an $.ajax({ POST.....

[{"total_votes":1,"options":[{"id":40,"vote_count":0,"users":[]},{"id":41,"vote_count":1,"users":[{"photo":"xxxxxxxxxxx.png","name":"XXXXX,"id":1}]},{"id":42,"vote_count":0,"users":[]}]}]

so I try to get total_votes with:

    success: function(e) {
        console.log(e['total_votes'])       
    }

also try to get

        console.log( e['options'].length() )
        console.log( e['options'][0]['id'] )

Suggestions on why I keep getting undefined? Also is there a better way to loop through options?

Thanks

Share Improve this question edited Jun 26, 2011 at 18:40 user113716 323k64 gold badges453 silver badges441 bronze badges asked Jun 26, 2011 at 18:29 AnApprenticeAnApprentice 111k202 gold badges636 silver badges1k bronze badges 2
  • You excluded some important details from your $.ajax() method. Did you set the dataType: property to 'json'? If not, is it being sent from the server with the proper headers? – user113716 Commented Jun 26, 2011 at 18:42
  • @AnApprentce, I think ur Json is not correctly formatted. you may need to check the returned type using firbugs in firefox. – tkt986 Commented Jun 26, 2011 at 18:51
Add a ment  | 

5 Answers 5

Reset to default 8

Your root object is an array, so you would need to do something like e[0]['total_votes']. Also the length of an array is not a function its a property so you would want to do e[0].options.length or e[0]['options'].length.

Loop through them with $.each()

It would be e[0].total_votes according to your example JSON response (it's in an array).

You need to call JSON.parse before dereferencing the JSON object

Formatted ur Json this way, you can get the "total_votes" value like this

success: function(e) {
    console.log(e[0].total_votes);

}

 [
    {
        "total_votes": 1,
        "options": [
            {
                "id": 40,
                "vote_count": 0,
                "users": []
            },
            {
                "id": 41,
                "vote_count": 1,
                "users": [
                    {
                        "photo": "xxxxxxxxxxx.png",
                        "name": "XXXXX",
                        "id": 1
                    }
                ]
            },
            {
                "id": 42,
                "vote_count": 0,
                "users": [
                    {}
                ]
            }
        ]
    }
]

check out here

发布评论

评论列表(0)

  1. 暂无评论