JS cannot parse JSON with unicode character

人走茶凉 提交于 2021-02-18 22:34:40

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!