我有一个asp的 JsEncode
Function jsEncode(str)
Dim i, j, aL1, aL2, c, p aL1 = Array(&h22, &h5C, &h2F, &h08, &h0C, &h0A, &h0D, &h09)
aL2 = Array(&h22, &h5C, &h2F, &h62, &h66, &h6E, &h72, &h74)
For i = 1 To Len(str)
p = True
c = Mid(str, i, 1)
For j = 0 To 7
If c = Chr(aL1(j)) Then
jsEncode = jsEncode & "\" & Chr(aL2(j))
p = False
Exit For
End If
Next If p Then
Dim a
a = AscW(c)
If a > 31 And a < 127 Then
jsEncode = jsEncode & c
ElseIf a > -1 Or a < 65535 Then
jsEncode = jsEncode & "\u" & String(4 - Len(Hex(a)), "0") & Hex(a)
End If
End If
Next
End Function不知道,C#怎么去写

解决方案 »

  1.   

    C#有现成的函数:
    Server.HtmlEncode(str);
    HttpUtility.HtmlEncode(str);
      

  2.   


    没有效果,这个我知道,但是,我需要的是序列化成安全的Js 字符串例如:
    Json数据转换出来是
    {site_com_intro:"<p><embed type=\"application\/x-shockwave-flash\" src=\"http:\/\/127.0.0.1\/www\/Thinkart\/Mr_Think\/TK-Admin\/lib\/upfile\/2009122949736365.swf\" wmode=\"opaque\" quality=\"high\" bgcolor=\"#ffffff\" menu=\"false\" play=\"true\" loop=\"true\" height=\"100\" width=\"200\" \/><\/p><p><br \/><\/p><p><a href=\"http:\/\/127.0.0.1\/www\/Thinkart\/Mr_Think\/TK-Admin\/lib\/upfile\/2009122949757449.txt\">\u70B9\u51FB\u6253\u5F00\u94FE\u63A5<\/a><br \/><\/p>"}