Processing of single quotes and special strings when processing Json data using asp.net

Source: Internet
Author: User

In the json processing of asp tutorial. net, single quotes or other special characters are often used. If you use them directly, json data is truncated, causing js errors to fail to run correctly.

In fact, you only need to use the escape method of the webpage special effect to encode the string and then solve this problem.

 

But how can we solve this problem in c # code?

We naturally thought of using the server. urlencode method to encode data. However, after practice, we found that some characters After encoding cannot be restored by the unescape method in js.

I will use the server. urlencode function in. net for conversion. However, this still does not work. The reason was not found after the experiment for a long time. Later I suspect that the server. urlencode function in the asp.net tutorial is different from the value returned by the server. urlencode function in asp. 1. Experiment. Actually, it is.

Test code:

What is the following code in asp.net? Response. write (server. urlencode ("assembly "));? Return Value: % e6 % b1 % 87% e7 % bc % 96

The following code in asp: response. write server. urlencode ("assembly ")?? Return Value: % bb % e3 % b1 % e0


Cause of this problem: server. urlencode in asp.net is handled in UTF-8 encoding by default. In asp, the encoding method is set locally.

If you use the following encoding in asp.net: asp and asp.net, the results will be the same:

Response. write (httputility. urlencode ("assembly", encoding. default ));

Use: response. write (httputility. urlencode ("assembly", encoding. utf8 ));? Response. write (server. urlencode ("assembly "));?? Returned results

 

In fact, the solution is very simple. After referencing the microsoft. jscript assembly, you can use the escape method of the globalobject class.

This method works similar to server. urlencode, but it calls the javascript escape method in the c # code, which can be restored by the unescape method.

 

Eg.

. Cs

1 microsoft. jscript. globalobject. escape (strjs );
 

. Js

 

Var css tutorial tr = unescape(json.css tr );

Related Article

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.