Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS cannot parse JSON with unicode character

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

like image 591
Thanh Trung Avatar asked Jun 23 '15 16:06

Thanh Trung


People also ask

Can JSON handle Unicode?

(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.

Does JSON support special characters?

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.

Can JavaScript parse JSON natively?

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.

Is JSON Parsable JavaScript?

Parsing FunctionsFunctions are not allowed in JSON. If you need to include a function, write it as a string.


1 Answers

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.

like image 65
berkyl Avatar answered Sep 17 '22 15:09

berkyl