C # The background code encodes JSON.

Source: Internet
Author: User

Loading editor...

C # BackgroundCodeThis article Reprinted from http://www.cnblogs.com/ITniao/archive/2011/01/06/1929261.html (IT) bird in ASP.. Net JSON processing usually involves the use of single quotes or other special characters. If you use it directly, the JSON data will be truncated, causing JS errors to fail to run correctly. In fact, you only need to use the Javascript escape method to encode the string to 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. In fact, the solution is very simple. Reference Microsoft. JScriptProgramSet, 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.

Microsoft. JScript. globalobject. Escape (strjs) in. CS );

VaR csstr = Unescape (JSON. csstr );

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.