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.
Eg.
. CS
1 Microsoft. JScript. globalobject. Escape (strjs );
. Js
VaR Csstr = Unescape (JSON. csstr );