3

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 :

"projects":[
    {
        "id": "1",
        "title": "CURSOS DE PERIODISME",
        "description": "Els cursos tenen l\u0092objectiu d\u0092aprofundir en l\u0092actitud period\u00edstica dels alumnes."
    }
]

When I use the jquery.parseJSON and print the text again into the description, the text is parsed as follows:

Els cursos tenen lobjectiu daprofundir en lactitud periodística dels alumnes.

All other unicode characters are well parsed, but why \u0092 is not parsed? What I'm doing wrong?

Thanks in advance!

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875

2 Answers2

2

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.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Thanks for the answer, and what can I do to display it? I would like to see : Els cursos tenen l'objectiu d'aprofundir – Sergi Mínguez Jun 14 '12 at 12:02
  • 2
    @SergiMínguez: `str = str.replace(/\u0092/g, "'")` should do it: [Live example](http://jsbin.com/igocof/2) | [source](http://jsbin.com/igocof/2/edit) – T.J. Crowder Jun 14 '12 at 12:06
0

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.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375