Save php json object into jquery variables
<p>I would take a php json object and save it in a jquery variable.</p>

<pre><code>&lt;?php
$arr = array();
for ($i = 0; $i &lt; 5; $i++) {
    $arr[] = array('id'=&gt;$i, 'text'=&gt;$i);
}
$arr = json_encode($arr);
?&gt;
&lt;input id="phpObj" type="hidden" value="" data-items='&lt;?php echo $arr; ?&gt;'&gt;
</code></pre>

<p>browser html source display</p>

<pre><code>&lt;input id="phpObj" type="hidden" data-items="[{"id":0,"text":0},{"id":1,"text":1},{"id":2,"text":2},{"id":3,"text":3},{"id":4,"text":4}]"&gt;
</code></pre>

<p>For get the php object I did it this way</p>

<pre><code>var data = jQuery.parseJSON($('#phpObj').data('items'));
console.log(data);
</code></pre>

<p>but I've this error</p>

<pre><code>SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data
</code></pre>

<p>I tried to use <code>jQuery.parseJSON(JSON.stringify($('#phpObj').data('items')));</code>
but with no success. <code>SyntaxError: unterminated string literal</code></p>

<p>How can I solve this? Thanks</p>