开发者

How can I get this string to convert back into the array?

开发者 https://www.devze.com 2023-01-06 23:26 出处:网络
I am trying to use jMonthCalendar to add some events from an XML feed into a calendar.In the original jMonthCalendar, the events are in an array that looks like this:

I am trying to use jMonthCalendar to add some events from an XML feed into a calendar. In the original jMonthCalendar, the events are in an array that looks like this:

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" }];

I am using a loop to create a bunch开发者_StackOverflow社区 of events like this:

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

I am then trying to get these back into an array by performing

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

The problem is, the stuff in "eventsArray" does not get converted back into array objects, like it does in the example source.

I know that this is a noob question, but any help would be appreciated.


Instead of using += and the string version of the object, try appending the actual object.

For example, instead of:

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

Do:

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


Change your creation loop:

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


I believe you will get what you want by switching from string concatenation to manipulating the objects directly:

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

精彩评论

暂无评论...
验证码 换一张
取 消