Transfer: https://www.cnblogs.com/ITniao/archive/2011/01/06/1929261.html
Single and processing special character string types when Json .NET
In the process Json Asp.NET of commonly encountered use single quotation marks or other special characters, if used directly, it truncates Json data, causing JS errors can not run properly.
In fact, as long as the encoding method using the escape after a string of Javascript can solve this problem.
But how to solve this problem in C # code it?
We naturally think of using Server.UrlEncode method to encode data, but the practice found that some characters can not be encoded reduction in JS unescape method.
In fact, the solution is simple, the reference Microsoft.JScript assembly, use escape method GlobalObject class on the line.
This method is similar to the role and Server.UrlEncode, but it is the equivalent of calling the javascript escape method in C # code, naturally can guarantee unescape method that can be restored.
eg.
.cs in
.js in
In the process Json Asp.NET of commonly encountered use single quotation marks or other special characters, if used directly, it truncates Json data, causing JS errors can not run properly.
In fact, as long as the encoding method using the escape after a string of Javascript can solve this problem.
But how to solve this problem in C # code it?
We naturally think of using Server.UrlEncode method to encode data, but the practice found that some characters can not be encoded reduction in JS unescape method.
In fact, the solution is simple, the reference Microsoft.JScript assembly, use escape method GlobalObject class on the line.
This method is similar to the role and Server.UrlEncode, but it is the equivalent of calling the javascript escape method in C # code, naturally can guarantee unescape method that can be restored.
eg.
.cs in
.js in