Skip to content Skip to sidebar Skip to footer

How To Correct Character Encoding In IE8 Native Json?

I am using json with unicode text, and am having a problem with the IE8 native json implementation. &l

Solution 1:

To answer my own question - Apparently this is not natively possible in IE8, but it does work correctly in the IE9 Beta.

A fix is possible though:

<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 alert() back the original string on all of IE, FF and Chrome.


Solution 2:

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


Solution 3:

Ensure, your server is properly configured. Mine was replying, even for unicode JSON files:

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

Solution 4:

I think regexp:

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

is too aggresive. If you had a string '\u' in your input that wasn't the UTF character, it would still catch it.

I think what you need is this:

unescape(stringified.replace(/([^\\])\\u([0-9][0-9][0-9][0-9])/g,"$1%u$2"));

This would only change \uxxxx sequences if x is a digit and the whole sequence is not proceeded by a backslash (\).


Post a Comment for "How To Correct Character Encoding In IE8 Native Json?"