You are on the right track. You would just do something similar in your json action:
public function json(SS_HTTPRequest $request) { $f = new JSONDataFormatter(); $this->response->addHeader('Content-Type', 'application/json'); return $f->convertDataObject($this->dataRecord); }
Or for certain fields you can do this:
public function json(SS_HTTPRequest $request) { // Encode specific fields $data = array(); $data['ID'] = $this->dataRecord->ID; $data['Title'] = $this->dataRecord->Title; $data['Content'] = $this->dataRecord->Content; $this->response->addHeader('Content-Type', 'application/json'); return json_encode($data); }
If you put the above inside the controller into the Page.php file, and all other pages are expanded by Page_Controller , then you can go to http://mydomain/xxxx/json and get JSON output for any page.
source share