When processing of the .NET species Json single and special character string processing .NET Json species of special string of single and

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

1 Microsoft.JScript.GlobalObject.escape(strJS);

 

 .js in

 

var csStr = unescape(json.csStr);

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

1 Microsoft.JScript.GlobalObject.escape(strJS);

 

 .js in

 

var csStr = unescape(json.csStr);

Guess you like

Origin www.cnblogs.com/jiangdd/p/11936044.html