How to fix character encoding in native json IE8?

I am using json with unicode text and I had a problem with json based json implementation.

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script> var stringified = JSON.stringify("สวัสดี olé"); alert(stringified); </script> 

Using json2.js or native json FireFox, the alert() is the same as the original. IE8, on the other hand, returns Unicode values, not the original text \u0e2a\u0e27\u0e31\u0e2a\u0e14\u0e35 ol\u00e9 . Is there an easy way to get IE to behave like the others or to convert this string to how it should be? And do you think this is a mistake in IE, I thought that for the implementation of json2.js similar replacements should be used for native json?

Edit: play on jsfiddle using the above code - http://jsfiddle.net/vV4uz/

+7
json javascript internet-explorer encoding unicode
source share
4 answers

To answer my own question - apparently this is not possible initially in IE8, but it works correctly in IE9 beta.

A fix is possible:

 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script> var stringified = JSON.stringify("สวัสดี olé"); stringified = unescape(stringified.replace(/\\u/g, '%u')); alert(stringified); </script> 

Which will correctly warn () back the original line in all IE, FF and Chrome.

+7
source share

If this is before sending it to the server, you can first encode it encodeURIComponent (JSON.stringify ("สวัสดี olé")) and use the utf8 decoder on the server

+1
source share

Make sure your server is configured correctly. Mine answered, even for Unicode JSON files:

 Content-Type: text/html; charset=ISO-8859-1 
0
source share

I think regexp:

unescape (stringified.replace (/ \ u / g, '% u'));

too aggressive. If you had the string "\ u" in your input that was not a UTF character, it would catch it anyway.

I think you need the following:

escaping in (stringified.replace (/ ([^ \\]) \\ and ([0-9] [0-9] [0-9] [0-9]) / g, "$ 1% U $ 2" ));

This will only change \ uxxxx sequences if x is a digit and the entire sequence will not be executed with a backslash (\).

0
source share

All Articles