In the json processing of asp tutorial. 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 escape method of the webpage special effect to encode the string and then solve this problem.
But how can we solve this problem in c # code?
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.
I will use the server. urlencode function in. net for conversion. However, this still does not work. The reason was not found after the experiment for a long time. Later I suspect that the server. urlencode function in the asp.net tutorial is different from the value returned by the server. urlencode function in asp. 1. Experiment. Actually, it is.
Test code:
What is the following code in asp.net? Response. write (server. urlencode ("assembly "));? Return Value: % e6 % b1 % 87% e7 % bc % 96
The following code in asp: response. write server. urlencode ("assembly ")?? Return Value: % bb % e3 % b1 % e0
Cause of this problem: server. urlencode in asp.net is handled in UTF-8 encoding by default. In asp, the encoding method is set locally.
If you use the following encoding in asp.net: asp and asp.net, the results will be the same:
Response. write (httputility. urlencode ("assembly", encoding. default ));
Use: response. write (httputility. urlencode ("assembly", encoding. utf8 ));? Response. write (server. urlencode ("assembly "));?? Returned results
In fact, the solution is very simple. After referencing the microsoft. jscript assembly, you can 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 css tutorial tr = unescape(json.css tr );