title: How to output JSON parse compatible json with PHP json_encode? JSON.parse - Unexpected token h due to single escaped double quotes
body: <p>I have this php:</p>
  
  <pre><code>&lt;?php
  
      $jsonarr = array("haha" =&gt; array("hihi'hih","hu\"huh"));
      print json_encode($jsonarr);
  </code></pre>
  
  <p>This gives me</p>
  
  <pre><code>{"haha":["hihi'hih","hu\"huh"]}
  </code></pre>
  
  <p>Now, in JSON.parse this breaks with</p>
  
  <pre><code>Uncaught SyntaxError: Unexpected token h
      at Object.parse (native)
  </code></pre>
  
  <p>, unless I double escape the backslash like this</p>
  
  <pre><code>var json = '{"haha":{"hihi\'hih":"hu\\\"huh"}}';
  JSON.parse(json);
  </code></pre>
  
  <p>How can I get php to create a JSON parse compatible output?</p>
  
  <p>Meanwhile, I did get this to work with double json_encoding the string in php based on this <a href="https://stackoverflow.com/questions/1048487/phps-json-encode-does-not-escape-all-json-control-characters">PHP&#39;s json_encode does not escape all JSON control characters</a> like this, but wonder if there is some other way.</p>
  
  <pre><code>$jsonarr = array("haha" =&gt; array("hihi'hih","hu\"huh"));
          print json_encode(json_encode($jsonarr));
  </code></pre>