2010-07-08 82 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); 
相關問題