问题
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
回答1:
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.
回答2:
You need to escape your "
and escape the \
:
json = '{"name":""C\u008cUR Carmen"}';
should be
json = '{"name":"\\"C\\u008cUR Carmen"}';
If the 4th "
is just a typing an error, just escape the \
that:
json = '{"name":"C\\u008cUR Carmen"}';
来源:https://stackoverflow.com/questions/31008002/js-cannot-parse-json-with-unicode-character