jQuery.parseJSON \u0092 character is not parsed

前端 未结 2 1301
走了就别回头了
走了就别回头了 2021-01-20 05:13

I have a text encoded from php with an ajax function with de php utf8_encode.If I print it in the console directly the text is displayed as follows :

\"proj         


        
相关标签:
2条回答
  • 2021-01-20 05:44

    It is parsed by jQuery. A simple test can show you:

    > $.parseJSON('"\\u0092"').length 
    1
    > $.parseJSON('"\\u0092"').charCodeAt(0)
    146
    > $.parseJSON('"\\u0092"').charCodeAt(0).toString(16)
    "92"
    

    It only won't get displayed, see @TJCrowders answer for that.

    0 讨论(0)
  • 2021-01-20 05:52

    U+0092 is a control character, perhaps it's being parsed but you're not seeing it because of how you're using the string.

    For example, this code which does no JSON parsing at all:

    (function() {
    
      var strWith = "Els cursos tenen l\u0092objectiu d\u0092aprofundir";
      var strWithout = "Els cursos tenen lobjectiu daprofundir";
    
      display("With    (" + strWith.length + "): " + strWith);
      display("Without (" + strWithout.length + "): " + strWithout);
    
      function display(msg) {
        var p = document.createElement('pre');
        p.innerHTML = String(msg);
        document.body.appendChild(p);
      }
    })();
    

    Live copy | source

    Output:

    With    (40): Els cursos tenen lobjectiu daprofundir
    Without (38): Els cursos tenen lobjectiu daprofundir

    As you can see, they look the same with and without the control character, but we can see from the lengths that the control character is included in the string.

    0 讨论(0)
提交回复
热议问题