I will post my experience with PHP. I hope it could helps.
Usually, one use the json_encode()
function to encode the data, e.g.:
json_encode(array('data1' => 'String data with text',
'data2' => '<a href="www.stackoverflow.com">The Site</a>'));
Since json_encode()
works fine only with UTF-8 strings, I suggest to encode every string in UTF-8 through the function utf8_encode()
, i.e.
json_encode(array(utf8_encode('data1') => utf8_encode('String data with text'),
utf8_encode('data2') => utf8_encode('<a href="www.stackoverflow.com">The Site</a>')));
Moreover, if you are using special chars (like è and à in Italian words), I suggest to decode the returned json UTF8 encoded HTML. This is particularly useful if you need to use it in an HTML page (e.g. as a result of an AJAX call). To decode through Javascript, use:
decodeURIComponent(escape(html));
where html
is the returned encoded HTML code.
Regards.