I am trying to create a valid JSON string so that I can use Jquery .ajax to pull some events into different places on the web. I created a method on my EventsPage_Controller called jsonDates
public function jsonDates() {
$events = DataObject::get('Event');
$output = array(); $i = 0;
foreach($events as $event) {
$output[$i] = array(
'start' => $event->EventDate,
'title' => $event->EventTitle,
'id' => $event->ID,
'thumb' => $event->EventThumbnail()->Link(),
'cover' => $event->Image()->Link(),
'desc' => $event->Desc,
'link' => $event->EventLink,
);
$i++;
}
return json_encode($output);
}
Here's a snippet of the JSON i get when I navigate to mysite.com/events/jsonDates:
{"start":"2011-09-16 16:33:09","title":"Test Event","id":30,"thumb":"\/mysite.com\/assets\/","cover":"\/mysite.com\/assets\/","desc":"
This is a test event.<\/p>","link":"http:\/\/google.com"}
When I run the snippet above through JSON Lint, it seems to be choking on the space that the TinyMCE editor is placing in my description. There also seems to be an issue with the closing tags. I am getting this error:
Parse error on line 7:
...ets\/", "desc": "This is a test eve
----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['
Is this an issue of the CMS editor introducing unnecessary spaces?
Is there a better way of creating urls of JSON data using Silverstripe?