Solutions for automatic conversion of Unicode characters by json.stringify method in IE8

Source: Internet
Author: User
Tags string back

The IE8 has a built-in JSON object for working with JSON data. Unlike the standard method, the IE8 json.stringify will transcode the Utf-8 characters:

    var str = "I am a programmer"    var//"" \u6211\u662f\u7a0b\u5e8f\u5458 "

In general, we call this method when we send data to the server. The server side can parse correctly, so there is no problem. But if we jsonstr to use it somewhere else, we need to do some processing.

One way is to use eval:

    var // "I'm a programmer."

However, when we call str more than once, is this method OK:

    var str = ' I am a programmer '    var jsonstr = json.stringify (str    )var//    var another_parsed = eval (' (' +another_jsonstr+ ') ')    //"" \u6211\u662f\ u7a0b\u5e8f\u5458 ""

It turns out to be no good, then we write a method, if we find that the result of parsing has a utf-8 code, we think that the string is not resolved completely, continue to call Eval parsing:

function Ie8parse (JSON) {        var re =/\\u[0-9a-fa-f]{4}/,            result        = eval (' (' + JSON + ') ' while (  re.test (Result))            {= eval (' (' + result + ') }                 return  result    }
Ie8parse (ANOTHER_JSONSTR)//"I am a programmer"

Another method is to do some processing of the IE8 Json.stringify method and convert the Utf-8 code in the converted string back to the character:

    function Stringify (object) {        var string = json.stringify (object)        return String.Replace (/\\u ([0-9a-fa-f]{2,4})/g,function(string,matched) {            return String.fromCharCode (parseint (matched,16)        })    }

In this way, you can solve the IE8 json.stringify automatic conversion utf-8 characters caused by the inconvenience.

Solutions for automatic conversion of Unicode characters by json.stringify method in IE8

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.