How does Discuz filter special JSON characters?

Source: Internet
Author: User

When using JSON, some special characters make us unable to wait for the expected results. The following filtering method uses the filtering method in Discuz to help us solve this problem. First, the js code that handles this problem is attached with the front-end code.

/// <Summary> /// load the event /// </summary> /// <param name = "sender"> </param> /// <param name = "e"> </param> protected void Page_Load (object sender, eventArgs e) {if (! IsPostBack) {FileStream fsSteam = new FileStream (Server. MapPath ("~ /SQLLog.txt "), FileMode. Open); // The text in SQLLog.txt is" happy "! StreamReader sr = new StreamReader (fsSteam, System. Text. Encoding. GetEncoding ("gb2312"); string message = JsonCharFilter (sr. ReadToEnd (); // The same in bytes. Sr. close (); fsSteam. dispose (); string json = "{IfSuccess: true, Message: '" + message + "'}"; this. ltMessage. text = json; }}/// <summary> // filter Json characters, see http://www.json.org//// </summary> /// <param name = "sourceStr"> source string to be filtered </param> /// <returns> returns the filtered string </returns> public static string JsonCharFilter (string sourceStr) {sourceStr = sourceStr. replace ("\", "\"); sourceStr = sourceStr. replace ("B", "\ B"); sourceStr = sourceStr. replace ("t", "\ t"); sourceStr = sourceStr. replace ("n", "\ n"); sourceStr = sourceStr. replace ("n", "\ n"); sourceStr = sourceStr. replace ("f", "\ f"); sourceStr = sourceStr. replace ("r", "\ r"); return sourceStr. replace (""","\"");}

Front-end code:

<Html xmlns = "http://www.w3.org/1999/xhtml"> 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.