Notes for JSON. stringify and Unicode encoding

Source: Internet
Author: User

1json. stringify automatically converts Chinese characters in the content to be converted to unicode encoding
There are differences between two browsers. Some browsers will automatically convert the Unicode encoding in the content of the form to be submitted into Chinese characters (chrome will automatically convert, ie will not convert)
3 Web servers may also be treated differently. Others are unclear,Iis5 is not converted, iis7 is automatically converted(In other words, iis5 does not support SSI commands, and iis7 does ).

 

Browser-1-Submit Form-web server-2-Asp parser

Chrome transcodes the form before it is submitted to the server at 1.
Iis7 is transcoding before the form data is handed to the ASP parser at 2.

If there are Chinese characters in the content converted and submitted by JSON. stringify, special processing is required.
1. You don't have to worry about it. You can leave it to the Web server for processing.
2. Modify JSON. stringify. Check that JSCodeI gave up.
3. added the background Code Conversion and found one online.
Bytes

Public static string unicodetogb (string text) {system. text. regularexpressions. matchcollection MC = system. text. regularexpressions. regEx. matches (text, "\\\\ U ([\\ w] {4})"); string a = text. replace ("\ U", ""); char [] arr = new char [MC. count]; for (INT I = 0; I <arr. length; I ++) {arr [I] = (char) convert. toint32 (. substring (I * 4, 4), 16);} string c = new string (ARR); Return C ;}

 
Let's talk about my debugging experience.
The project content is online examination

JSON. stringify parses an object into a string
JSON. stringify (jsondata. Table) converts the "single choice" in the table to "\ u5355 \ u9009" and submits it as a JSON string.

The data returned during ie debugging vs debugging is
"ID": "10337", "Answer": "", "rightanswer": "C", "type": "\ u5355 \ u9009"

The server converts JSON to datatable.
Datatable dt = jsontoable able (table );
The result of this method is not processed for Unicode encoding.

The first code.
String Qt = DT. Rows [I] ["\" Type \ ""]. tostring ();
If (Qt = "single choice ")
A question type is required for verification. The score is calculated. However, the data in the table is encoded as "\ u5355 \ u9009" and cannot be matched.

Because there are not many places to use, I want to change it directly.
If (Qt = @ "\ u5355 \ u9009 ");

Debugging is successful. You can get the score, but you will encounter problems later.

Local debugging is correct. It is also true when it is published to the local IIS, but an error occurs when it is published to the server (to be exact, it is not an error, but the statistical result is 0, calculate the score based on the question type. If the question type does not match, the question sentence score is 0, and the total score is 0)

I tried for more than an hour and couldn't find the problem. The local IIS debugging is correct. The server is a little faulty, and it's not long before you come to the company. You know.

I suddenly thought that's the case with IE. What about other browsers?

I want to see the results of chorm in various versions, such as Vs, local IIS, and server IIS.

Chrome is actually the result 0 locally, but although the result is wrong, it vaguely feels the error.
Without ie debugging, use Chrome debugging to check the breakpoint. The data returned by Chrome is "single choice", not ie "\ u5355 \ u9009 ".
When chrome submits data, it automatically converts unicode encoding to Chinese characters.
If (Qt = @ "\ u5355 \ u9009") results are obvious.
Therefore, the result is normal.
If (Qt = @ "\ u5355 \ u9009" | Qt = "single choice ")

It is also normal to publish it to the server. It must be that the IIS service on the server also automatically converts the Unicode encoding in the form to Chinese characters.

 

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.