0

While retrieving some data from an online db, I can't figure our why some characters get "misinterpreted".

I send the data from the server through a PHP app, like so:

header('Content-Type: application/json');
// tried also header('Content-Type: application/javascript');
// tried also header('Content-Type: application/json; charset=utf-8');
echo json_encode($object);

If I test the message, the JSON encoded string correctly contains the following string \u00ec, which represents the letter ì.

However, in the client, when I check the unparsed string, I see the following string instead: \u00c3\u00ac, which represent, respectively: Ã and ¬ (this happens with all special characters like èéàòùü etc.).

As a consequence, when i call JSON.parse() on the response, I see the wrong characters.

Any idea of what I'm doing wrong?

don
  • 4,113
  • 13
  • 45
  • 70
  • What is the content you are encoding in json?? – Niranjan N Raju Nov 20 '15 at 17:14
  • It is a quite complex array, but essentially everything is parsed in JSON correctly, that's why I included only the characters that are giving me troubles. For example, the string that I am looking at now to test the problem has the word `lunedì` (monday) in it, and that `ì` is giving me headaches... – don Nov 20 '15 at 17:16
  • before encoding in json, print the `$object` variable and check. – Niranjan N Raju Nov 20 '15 at 17:17
  • @NiranjanNRaju the word `lunedì` appears as `lunedì` – don Nov 20 '15 at 17:50

1 Answers1

1

If you're using PHP 5.4< you can pass JSON_UNESCAPED_UNICODE as the second parameter of the json_encode function.

Masious
  • 431
  • 5
  • 14
  • Nope, still on 5.3. I'm planning to upgrade, but thats sort of a long time thing, while right now I need to solve this quite in the short time – don Nov 20 '15 at 17:18
  • Then take a look at this one: http://stackoverflow.com/questions/24932572/how-to-save-a-json-as-unescaped-utf-8-in-php-5-3 – Masious Nov 20 '15 at 17:23
  • Thank's! I'm still testing it to figure out what's going on underhood, but as of now the problem seems to be fixed, which is great! :-). In case it might help anyone, I'm using the following header: `header('Content-Type: application/javascript; charset=utf-8');` – don Nov 20 '15 at 18:19