我有来自Google日历的Feed,它将事件作为JSON对象提供
以下是其中一个事件
的示例"entry": [{
"id": {
"$t": "https://www.google.com/calendar/feeds/0183mgrcm70ch155q5bq1de9v8%40group.calendar.google.com/public/full/526dopgom77cdg9jmish3se1gk"
},
"published": {
"$t": "2014-09-11T08:17:53.000Z"
},
"updated": {
"$t": "2014-09-11T08:20:57.000Z"
},
"category": [{
"scheme": "http://schemas.google.com/g/2005#kind",
"term": "http://schemas.google.com/g/2005#event"
}],
"title": {
"$t": "The Village",
"type": "text"
},
"content": {
"$t": "",
"type": "text"
},
"link": [{
"rel": "alternate",
"type": "text/html",
"href": "https://www.google.com/calendar/event?eid=NTI2ZG9wZ29tNzdjZGc5am1pc2gzc2UxZ2sgMDE4M21ncmNtNzBjaDE1NXE1YnExZGU5djhAZw",
"title": "alternate"
}, {
"rel": "self",
"type": "application/atom+xml",
"href": "https://www.google.com/calendar/feeds/0183mgrcm70ch155q5bq1de9v8%40group.calendar.google.com/public/full/526dopgom77cdg9jmish3se1gk"
}],
这是我到目前为止的Javascript
function GCalEvents(gcal_json_url) {
jQuery.getJSON(gcal_json_url, function(data){
// Parse and render each event
jQuery.each(data.feed.entry, function(i, item){
if(i == 0) {
jQuery("#gcal-events li").first().hide();
};
var event_link = item.link.href;
// event title
var event_title = item.title.$t;
// event contents
var event_contents = jQuery.trim(item.content.$t);
// make each separate line a new list item
event_contents = event_contents.replace(/\n/g,"</li><li>");
// event start date/time
var event_start_date = new Date(item.gd$when[0].startTime);
// if event has a start time (as oppose to all day), format date with time
if(event_start_date.getHours() != 0 || event_start_date.getMinutes() != 0) {
var event_start_day = event_start_date.toString("d");
var event_start_month = event_start_date.toString("MMM");
var event_start_time = event_start_date.toString("h:mm tt");
} else {
// otherwise format start as date only (without time)
var event_start_day = event_start_date.toString("d");
var event_start_month = event_start_date.toString("MMM");
};
// event location - if not null, surround with parens
var event_loc = item.gd$where[0].valueString;
// Render the event
jQuery("#gcal-events li").last().before(
"<div id=\"calentry\">"
+ "<ul>"
+ "<a href=\"" + event_link + "\"><li class=\"eldate\"> <span id=\"elmonth\">" + event_start_month + "</span><br><span id=\"elday\">" + event_start_day + "</span><br>" + event_start_time + "</li></a>"
+ "<li><span class=\"elvenue\">" + event_title + "</span><br><span class=\"ellocation\">" + event_loc + "</span></li>"
+ "</ul>"
+ "<div id=\"clearfix\"></div>"
+ "</div>"
);
});
});
}
我已经成功地将信息提取到event_title,event_contents和event_start_date变量中,但event_link变量一直未定义(我试图从&#34; link:href&#34;到这个变量)
有什么想法吗?
答案 0 :(得分:0)
item.link
是一个数组。您可以使用此特定JSON,例如item.link[0].href
或item.link[1].href
。
答案 1 :(得分:0)
您的&#34;链接&#34;变量是一个数组。因此,您必须执行以下操作才能访问值:
var event_link = item.link[0].href; // This gives you this first one
更好的方法是检查是否有一个:
var event_link = null;
if (item.link.length > 0)
{
var event_link = item.link[0].href;
}
答案 2 :(得分:0)
以下代码对您有所帮助。
var dataObj = jQuery.parseJSON(data);
var event_link = null;
if (dataObj.link.length > 0)
{
var event_link = dataObj .link[0].href;
}