asp.net the processing of single quotes and special strings when processing JSON data

Source: Internet
Author: User
Tags urlencode

in ASP tutorial. NET's JSON processing, it is common to encounter the use of single quotes or other special characters, which, if used directly, truncate the JSON data, causing the JS error to run incorrectly.

In fact, as long as the use of the web effects of the escape method to encode the string can solve this problem.

But how do you solve the problem in C # code?

We naturally thought of using the Server.URLEncode method to encode data, but after practice found that some characters in the code in JS can not be unescape method restore.

I will use the Server.URLEncode function in. NET to convert. But that's still not going to work. The experiment was long and did not find a reason. The Server.URLEncode function in the ASP.net tutorial was later suspected to be different from the value returned by the Server.URLEncode function in ASP. An experiment. It really is.

Test code:

asp.net in the following code? Response.Write (Server.URLEncode ("Compendium"));? Return:%e6%b1%87%e7%bc%96

ASP in the following code Response.Write Server.URLEncode ("assembly")?? return:%BB%E3%B1%E0


The reason for this problem: the Server.URLEncode in asp.net is handled in the Utf-8 encoding mode by default. In the ASP, it is processed according to the local coding method.

If you use the following code under ASP.net: The results of ASP and asp.net will be the same:

Response.Write (Httputility.urlencode ("Assembly", Encoding.default));

Adopted: Response.Write (Httputility.urlencode ("Assembly", Encoding.UTF8)); The return is Response.Write (Server.URLEncode ("Compendium"));?? The result returned

In fact, the solution is very simple, after referencing the Microsoft.JScript assembly, use the Globalobject class of the escape method on the line.

This method works like Server.URLEncode, but it is equivalent to invoking JavaScript's Escape method in C # code, which is naturally guaranteed to be restored by the Unescape method.

eg.

. CS in

1 Microsoft.jscript.globalobject.escape (STRJS);

. js

var css Tutorial tr = unescape (JSON.CSSTR);

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.