Bogdan
Bogdan

Reputation: 47

JSON + Polish characters

I'm using JSON to send array from PHP to Javascript. The array is getting data form the database. I've searched multiple topics on problem with JSON and foreign characters and everybody says that to solve the problem you have to set encoding to utf-8. I have done it I think but it just not help me. So i run the test to see what is the problem. To make it easy and simple i try to pass the string with polish characters to json_encode() function. So i have created index.php file. In my text editor i choose option Encode in UTF-8 without BOM and i started to code:

<?php
 header('Content-Type: text/html; charset=utf-8');
 $data = "polish characters: ążśęłóćźć";
 $jsonData = json_encode($data);
 echo mb_detect_encoding($data) . "<br />;
 echo $jsonData;
?> 

The result is:

UTF-8
"polish characters : \u0105\u017c\u015b\u0119\u0142\u00f3\u0107\u017a\u0107"

I still can't output polish characters. I don't know what is wrong? Please Help :)

Upvotes: 1

Views: 6282

Answers (1)

DJG
DJG

Reputation: 6543

Try doing:

echo json_decode(json_encode($data));

You should see that the Polish characters are displayed just fine.

Applications should not display JSON directly to the end user, but should first parse it to a more human friendly format. If you follow that rule, json_encode should not give you any issues, as you will always decode and parse it into any format that is appropriate.

Upvotes: 1

Related Questions