jQuery.parseJSON \u0092 character is not parsed
Asked Answered
M

2

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!

Maxi answered 14/6, 2012 at 10:6 Comment(3)
What is your original string? Tried removing utf8_encode?Melonymelos
Just a remark: FF14 JSON.parse parses your string correctly including \u0092.Lakeishalakeland
If I romve the uf8_encode returns null with the json_encodeRubierubiginous
S
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.

Strobilaceous answered 14/6, 2012 at 10:18 Comment(2)
Thanks for the answer, and what can I do to display it? I would like to see : Els cursos tenen l'objectiu d'aprofundirRubierubiginous
@SergiMínguez: str = str.replace(/\u0092/g, "'") should do it: Live example | sourceStrobilaceous
C
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.

Condemn answered 14/6, 2012 at 11:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.