in ASP tutorial. NET's JSON processing, it is common to encounter the use of single quotes or other special characters, which, if used directly, truncate the JSON data, causing the JS error to run incorrectly.
In fact, as long as the use of the web effects of the escape method to encode the string can solve this problem.
But how do you solve the problem in C # code?
We naturally thought of using the Server.URLEncode method to encode data, but after practice found that some characters in the code in JS can not be unescape method restore.
I will use the Server.URLEncode function in. NET to convert. But that's still not going to work. The experiment was long and did not find a reason. The Server.URLEncode function in the ASP.net tutorial was later suspected to be different from the value returned by the Server.URLEncode function in ASP. An experiment. It really is.
Test code:
asp.net in the following code? Response.Write (Server.URLEncode ("Compendium"));? Return:%e6%b1%87%e7%bc%96
ASP in the following code Response.Write Server.URLEncode ("assembly")?? return:%BB%E3%B1%E0
The reason for this problem: the Server.URLEncode in asp.net is handled in the Utf-8 encoding mode by default. In the ASP, it is processed according to the local coding method.
If you use the following code under ASP.net: The results of ASP and asp.net will be the same:
Response.Write (Httputility.urlencode ("Assembly", Encoding.default));
Adopted: Response.Write (Httputility.urlencode ("Assembly", Encoding.UTF8)); The return is Response.Write (Server.URLEncode ("Compendium"));?? The result returned
In fact, the solution is very simple, after referencing the Microsoft.JScript assembly, use the Globalobject class of the escape method on the line.
This method works like Server.URLEncode, but it is equivalent to invoking JavaScript's Escape method in C # code, which is naturally guaranteed to be restored by the Unescape method.
eg.
. CS in
1 Microsoft.jscript.globalobject.escape (STRJS);
. js
var css Tutorial tr = unescape (JSON.CSSTR);