1

I'm using the following code to get JSON from a page and parse it to my HTML page. The JSON is not being displayed in UTF-8 and therefore has weird characters. How can I remove the weird characters?

$.get("test.php", function (data) {
    var JSON = jQuery.parseJSON(data);
    $.each(JSON.deals.items, function (index, value) {
        // My code
    });
});
Papak
  • 85
  • 9
JohnyChew
  • 147
  • 1
  • 2
  • 10

1 Answers1

6

Please make sure that every single step of your process is in UTF-8:

  • The database connection
  • The database tables
  • Your PHP file
  • The content-type headers that you output

    header('Content-Type: application/json; charset=utf-8');

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

Refer to: PHP json_encode json_decode UTF-8

EDIT: If you already have weird characters like \u00a3 in your JSON feed instead of the £ symbol, your problem has already been answered: https://stackoverflow.com/questions/1423846/convert-unicode-from-json-string-with-php

bruntime
  • 371
  • 2
  • 13
Laurens Swart
  • 1,234
  • 9
  • 24
  • there is no database . The php file has the content type header – JohnyChew Feb 02 '16 at 14:53
  • Well, assuming you're referring to the weird characters and escaped slashes in your data, your PHP file is getting it's data from somewhere, and this data is already not UTF-8... so your problem lies in wherever this PHP file get's its data from. – Laurens Swart Feb 02 '16 at 15:12
  • right , but i cannot change that . is there a way to remove the characters after? – JohnyChew Feb 02 '16 at 15:13
  • You are referring to the weird characters like `\u00a3` which should be £? If so, please refer to http://stackoverflow.com/questions/14523846/convert-unicode-from-json-string-with-php . Your problem has already been asked there, and has already been solved as well. I have updated my answer so you can select it as the correct one. – Laurens Swart Feb 02 '16 at 15:18