Practical record. Net processes single quotes and special strings in JSON data

Source: Internet
Author: User

This article describes how to process single quotes and special strings when. net uses JSON.

In JSON processing of ASP. 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 Javascript escape method to encode the string to solve this problem.


But in C #CodeHow to solve this problem?

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.



. CS

1 Microsoft. JScript. globalobject. Escape (strjs );


. Js


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: 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.