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

javascript - IntrepretingParsing JSON data with jQuery getJSON - Stack Overflow

programmeradmin1浏览0评论

I would like to interpret data from JSON feed using jQuery getJSON.

$(function() {
    $.getJSON(';callback=showMyVideos2&max-results=30', function(data) { 
        $.each(data.feed.entry, function(i, item) {
            updated = item.updated;
            url = item['media$group']['media$content']['url'];
            thumb = item['media$group']['media$thumbnail'][0]['url'];
            numViews = item['yt$statistics']['viewCount'];
        });
    });
});

How to correctly interpret JSON data and assign variables to data items (ex. url, numViews, etc...)? Thanks much in advance for any help.

I would like to interpret data from JSON feed using jQuery getJSON.

$(function() {
    $.getJSON('http://gdata.youtube./feeds/users/raywilliamjohnson/uploads?alt=json-in-script&callback=showMyVideos2&max-results=30', function(data) { 
        $.each(data.feed.entry, function(i, item) {
            updated = item.updated;
            url = item['media$group']['media$content']['url'];
            thumb = item['media$group']['media$thumbnail'][0]['url'];
            numViews = item['yt$statistics']['viewCount'];
        });
    });
});

How to correctly interpret JSON data and assign variables to data items (ex. url, numViews, etc...)? Thanks much in advance for any help.

Share Improve this question edited Jan 1, 2010 at 1:46 ChaosPandion 78.4k18 gold badges121 silver badges159 bronze badges asked Jan 1, 2010 at 1:43 SteveSteve 5,19414 gold badges51 silver badges65 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 4

You need to set the callback GET parameter to ? (callback=?), so jQuery will be able to make the JSONP request correctly and execute your callback.

Also to get the url, you need to access the item at index [0], just like you get the thumb:

$(function() {
    $.getJSON('http://gdata.youtube./feeds/users/raywilliamjohnson/uploads?alt=json-in-script&callback=?&max-results=30', function(data) { 
        $.each(data.feed.entry, function(i, item) {
            var updated = item.updated;
            var url = item['media$group']['media$content'][0]['url'];
            var thumb = item['media$group']['media$thumbnail'][0]['url'];
            var numViews = item['yt$statistics']['viewCount'];
            // ...
        });
    });
});

Check an example here.

发布评论

评论列表(0)

  1. 暂无评论