A few things to get you started:
JSON is "JavaScript Object Notation". Though it is used in many other languages, JavaScript understands it natively.
This means that once you've got a JSON string, you can get a JavaScript object out of that directly. If the JSON string represents a single object, you'll get a single JavaScript object. If it represents an array, you'll get a JavaScript array of JavaScript objects. If it is some nested structure, then you'll get just that. In a modern browser all you need on the JavaScript side is
JSON.parse(...) to do the magic.
To get the JSON into JavaScript, either:
Get it in JavaScript directly, using XMLHttpRequest or helpers such as jQuery's $.get. That will need you to understand some asynchronous programming, so maybe indeed one of the following is easier to start with:
Get it in PHP, parse it like you tried in your question, and then generate proper JavaScript to create a JavaScript object or array again. Note that PHP's json_decode
gets you some associative array, which you then need to map to a JavaScript object.
Get it in PHP, do not parse it at all, and simply forward the JSON string to JavaScript and parse it there, using JSON.parse
.
Get it in PHP, use Jim's simple solution to get it into JavaScript.
When generating JavaScript in PHP, you need to be careful with quotes, newlines and other special characters in string values. Like if the JSON is:
{"quote": "She said: 'Beware', and walked off"}
...then you cannot just concatenate text into obj.push('...')
as that would create invalid JavaScript:
obj.push('She said: 'Beware', and walked off');
Above, JavaScript does not know what to do with the text after the second single quote, and throws Uncaught SyntaxError: missing ) after argument list
. Likewise, newlines may be troublesome, like when unexpectedly getting a PHP error while generating the JavaScript (for reasons explained in Osama's answer), which will yield invalid JavaScript and throw Invalid or unexpected token
:
obj.push('<br />
<b>Notice</b>: Array to string conversion
in <b>myPhp.php</b> on line <b>20</b><br /> Array');
In the JSON example above, you could use double quotes in obj.push("...")
, to generate:
obj.push("She said: 'Beware', and walked off");
But in general you might not know what values you get, so you need to "escape" troublesome characters.
I don't know enough about PHP to know what's the best way to escape the strings. As (valid) JSON uses double quotes, it should already have escaped double quotes when needed. So, a JSON string might look like:
{"quote": "She said: \"Beware\", and walked off.\n\nWe'll remember her."}
Above, you need to take care of the backslashes that JSON already added for escaping. So PHP's addslashes might do, bus I did not test this:
<script type="text/javascript">
var obj = JSON.parse("<?= addslashes($data) ?>");
</script>
Otherwise, when first parsing $data
into a PHP object using json_decode
(or when not even doing that!), Jim's simple solution is certainly preferred.
Notice: Array to string conversion in myPhp.php on line 20
Array'); – maziarser Sep 16 '17 at 18:01