I have created a new JSON string (by converting a PHP array using json_encode) and stored it in $siteTree ... outputting it to the log shows the following
[
{
"attr": {
"id": "node_1",
"rel": "folder"
},
"data": "New Title",
"children": [
{
"attr": {
"id": "node_2",
"rel": "folder"
},
"data": "second document",
"children": []
}
]
}
]
So I know the JSON is formed correctly. What I am trying to do is use that variable in a javascript function to create an object using the JSON data ... I am doing the following in the template
<?php
use_helper('JavascriptBase');
echo javascript_tag('createTree('.$siteTree.')');
?>
The following is shown on the output page
createTree([{"attr":{"id":"node_1","rel":"folder"},"data":"New Title","children":[{"attr":{"id":"node_2","rel":"folder"},"data":"second document","children":[]}]}])
The function is called but the JSON has replaced the " with "e;. How can I stop this ?
Thanks for your help !
javascript_tag() ?json_decodegenerates JSON, the way you currently insert it into JavaScript will make it a JavaScript object (actually an array in your case). I'm sure that is what you want, I just wanted to clarify that.