2010-07-08 83 views
0

我正在尝试使用jMonthCalendar将XML Feed中的一些事件添加到日历中。在原来的jMonthCalendar,该事件是一个数组,看起来像这样:我怎样才能得到这个字符串转换回数组?

var events = [ 
{ "EventID": 1, "StartDateTime": new Date(2009, 5, 12), "Title": "10:00 pm - EventTitle1", "URL": "#", "Description": "This is a sample event description", "CssClass": "Birthday" }, 
{ "EventID": 2, "StartDateTime": "2009-05-28T00:00:00.0000000", "Title": "9:30 pm - this is a much longer title", "URL": "#", "Description": "This is a sample event description", "CssClass": "Meeting" }]; 

我使用一个循环来创建这样一串事件:

eventsArray += '{"EventID":'+eventID+', "StartDateTime": '+new Date(formattedDate)+', "EndDateTime": '+new Date(formattedDate)+', "Title": "'+eventTitle+'", "URL": "'+detailURL+'","Description": "'+description+'"},' 

然后我试图让这些回的阵列通过执行

eventsArray = eventsArray.slice(0, -1); var events = [eventsArray]; 

的问题是,在“eventsArray”的东西没有得到转化回阵列对象,如它在例如源一样。

我知道这是一个noob问题,但任何帮助,将不胜感激。

回答

1

而不是使用+ =和对象的字符串版本,尝试附加实际的对象。

例如,而不是:

eventsArray += '{"EventID":'+eventID+', "StartDateTime": '+new Date(formattedDate)+', "EndDateTime": '+new Date(formattedDate)+', "Title": "'+eventTitle+'", "URL": "'+detailURL+'","Description": "'+description+'"},' 

务必:

events.push({"EventID":eventID, "StartDateTime": new Date(formattedDate), "EndDateTime": new Date(formattedDate), "Title": eventTitle, "URL": detailURL,"Description": description}); 
+0

哇,谢谢!我结束了使用推,而不是追加,但真棒的答案。谢谢! – mrr0ng 2010-07-08 23:01:51

0

更改您的创作循环:

eventsArray.push({ 
    EventID: eventID, 
    StartDateTime: new Date(formattedDate), 
    EndDateTime: new Date(formattedDate), 
    Title: eventTitle, 
    URL: detailURL, 
    Description: description 
}); 
0

我相信你会得到你想要的东西从字符串转换连接直接操作对象:

var newEvent = {"EventID": eventID, "StartDateTime": new Date(formattedDate), "EndDateTime": new Date(formattedDate), "Title": eventTitle, "URL": detailURL, "Description": description}; 
events.push(newEvent); 
相关问题