I have the following JSON string {"name":""C\u008cUR Carmen"}
but \u008c
is not parsed. It shows empty character instead.
json = '{"name":"C\u008cUR Carmen"}';
json = JSON && JSON.parse(json) || $.parseJSON(json);
Show : CUR Carmen
Expect : CŒUR Carmen
Please help.
* Note * : The JSON data is returned by the PHP server so there shouldn't be any syntax error because I used json_encode
and get the response from AJAX. It works with other characters such as à, é
but only this wierd character doesn't display properly
EDIT : Solved! It's not a JS problem it's a charset problem returned by MySQL. You can use mysql_set_charset('utf8')
before returning SQL data. Show \u0152
as expected
(in §3) JSON text SHALL be encoded in Unicode. The default encoding is UTF-8. (in §6) JSON may be represented using UTF-8, UTF-16, or UTF-32.
In JSON the only characters you must escape are \, ", and control codes. Thus in order to escape your structure, you'll need a JSON specific function.
JSON is a JavaScript-based object/value encoding format that looks very close to raw JavaScript and can be very easily parsed by JavaScript code because JavaScript can effectively evaluate a JSON string and re-materialize an object from it.
Parsing FunctionsFunctions are not allowed in JSON. If you need to include a function, write it as a string.
There's no need to escape an unicode character stated in RFC 4627
2.5. Strings
The representation of strings is similar to conventions used in the C family of programming languages. A string begins and ends with quotation marks. All Unicode characters may be placed within the quotation marks except for the characters that must be escaped: quotation mark, reverse solidus, and the control characters (U+0000 through U+001F).
You can use your unicode string directly:
json = '{"name":"CŒUR Carmen"}';
json = JSON && JSON.parse(json) || $.parseJSON(json);
I think there's a transcoding bug in your server side implementation where you change the output to ASCII before using json_encode
. It is a requirement of JSON that all data is encoded in Unicode.
Edit
In this fiddle there's an example how to revert the escaped unicode in javascript.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With