使用jQuery getJSON进行Intrepreting / Parsing JSON数据 [英] Intrepreting/Parsing JSON data with jQuery getJSON

查看:102
本文介绍了使用jQuery getJSON进行Intrepreting / Parsing JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用jQuery getJSON来解释来自JSON feed的数据。

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

$(function() {
    $.getJSON('http://gdata.youtube.com/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'];
        });
    });
});

如何正确解释JSON数据并将变量分配给数据项(例如url,numViews等) ..)?非常感谢您提供任何帮助。

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

推荐答案

您需要设置回调 GET参数到 callback =?),所以jQuery将能够正确地发出JSONP请求执行你的回调。

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.

另外要获得 url ,你需要访问索引<$ c $的项目c> [0] ,就像你得到 thumb

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.com/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'];
            // ...
        });
    });
});

查看示例这里

这篇关于使用jQuery getJSON进行Intrepreting / Parsing JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆