I have this php code which dynamically builds a JSON string and writes to a javascript string:
<?
$s = json_encode(array("id" => "1", "name" => 'myn"ame'));
?>
<script>
<?echo ("var js = '".$s."';");?>
var obj = $.parseJSON( js );
</script>
the JSON string looks just fine (" appears to be escaped ok):
var js = '{"id":"1","name":"myn\"ame"}';
but $.parsesJSON is failing, it seems to want:
var js = '{"id":"1","name":"myn\\"ame"}';
So how should I properly be escaping for this scenario? (obviously I'd like to cater for all control characters)