Solution to Json.stringify () automatic translation of Chinese into Unicode

Source: Internet
Author: User

Recently in the work, found in the IE8 under the Json.stringify () automatically translated into Unicode encoding, the original selected Chinese characters, to the background into a Unicode encoding, that is, the form of \u****. Finding the data found that, unlike the standard json.stringify (), the IE8 built-in json.stringify () automatically converts the encoding from Utf-8 to Unicode encoding, resulting in this similar garbled situation.

The solution is divided into two kinds, the first is to receive the data in the background, the data is again transcoded, re-converted to Utf-8, and then saved to the database, so that the data from the database will be removed to the front-end into the utf-8 format (that is, normal Chinese). The second is the use of eval () for transcoding in both IE8 and advanced browsers, and the final result is utf-8 encoding.

This article focuses on the second method, the use of the Eval () Transcoding method. On the MDN, the interpretation of Eval () is: eval() The argument is a string. If the string represents an expression, eval () evaluates the expression. If the parameter represents one or more JavaScript statements, then Eval () executes the statements.

var a = "Eat Xiang Xiang";  var unicodejsona = json.stringify ("a");  Unicodevar jsona = eval (' (' + Unicodejsona + ') '); Utf-8

As the code above, through eval () execution, forcibly Unicode transcoding to Utf-8, whether json.stringify () out of the result is Utf-8 or Unicode, unified into the UTF-8 format.

In fact, there is a third way to solve the problem-using json.stringify () in json2.js instead of IE8 json.stingify (). However, when a JSON object comes with a browser, the JSON object that comes with the browser is enabled instead of the JSON object in Json2.js. To solve this problem, you can change the JSON object in Json2.js to JSON2 object and change the Json.stringify () to Json2.stringify ().

The above methods can solve the problem of automatically translating Chinese into Unicode under IE8.

Solution to Json.stringify () automatic translation of Chinese into Unicode

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.