使用JavaScript解析RSS源并不适用于RSS源中的所有项目
问题描述:
因此,这是用于访问RSS源的测试代码。另外,here is the RSS feed我想解析。这不是该项目的最终设计,只是试图拼凑在一起。使用JavaScript解析RSS源并不适用于RSS源中的所有项目
它适用于我使用c.title,c.link零件,但是不是 c.description或c.pubDate零件。它只是说它是未定义的。
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>not finished yet</title>
<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>
<script type='text/javascript' >/*
/*
* jGFeed 1.0 - Google Feed API abstraction plugin for jQuery
*
* Copyright (c) 2009 jQuery HowTo
*
* Licensed under the GPL license:
* http://www.gnu.org/licenses/gpl.html
*
* URL:
* http://jquery-howto.blogspot.com
*
* Author URL:
* http://me.boo.uz
*
*/
(function ($) {
$.extend({
jGFeed: function (url, fnk, num, key) {
if (url == null) {
return false;
}
var gurl = "http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&callback=?&q=" + url;
if (num != null) {
gurl += "&num=" + num;
}
if (key != null) {
gurl += "&key=" + key;
}
$.getJSON(gurl, function (data) {
if (typeof fnk == "function") {
fnk.call(this, data.responseData.feed);
} else {
return false;
}
});
}
});
})(jQuery);</script>
<script type='text/javascript'>
$(window).load(function() {
$.jGFeed('http://www.scarletknights.com/rss/rss.asp?sportid=1',
function (feeds) {
if (!feeds) {
alert('Trouble getting RSS feed :(');
return false;
}
for (var i = 0; i < feeds.entries.length; i++) {
var entry = feeds.entries[i];
console.log(entry);
// Entry title
$('#results').append('<h1>' + entry.title + '</h1>' + '<br/>');
}
}, 10);
});
</script>
</head>
<body>
<div id="results"></div>
</body>
</html>
答
当你正在使用的饲料包括三个字段:title
,link
,description
和pubDate
,你传递的是通过谷歌的API数据。您可以查看完整文档here,但所需的字段被称为title
,link
,contentSnippet
和publishedDate
。
下面是您的代码的一个工作示例:http://jsfiddle.net/LacE5/3/
答
您在Feed本身和Google Feed API为您提供的(已处理)返回JSON之间感到困惑。请记住,Google的Feed API不会返回与Feed格式相同的格式。
看看the documentation for the JSON return你正在请求,你会看到entries
数组中的项目的可用属性;它们包括title
和link
,但不包括description
或pubDate
。 content
或contentSnippet
可能是最接近description
; publishedDate
将是来自Feed的pubDate
。
Google这样做是因为他们的Feed API可以解析比RSS更多的内容;它还解析Atom,并从两种类型的Feed中以一致的格式返回一个对象,以使生活更轻松。
啊谢谢。我的部分愚蠢的错误,我认为它返回的将是相同的格式。我将不得不检查其他RSS提要如何最终被格式化。 – Zeratas