json中含有Unicode的處理辦法 C#


public static class StringExtension  
{      
    #region unicode 字符轉義  
    /// <summary>  
    /// 轉換輸入字符串中的任何轉義字符。如:Unicode 的中文 \u8be5  
    /// </summary>  
    /// <param name="str"></param>  
    /// <returns></returns>  
    public static string UnicodeDencode(this string str)  
    {  
        if (string.IsNullOrWhiteSpace(str))  
            return str;  
        return Regex.Unescape(str);  
    }  
    /// <summary>  
    /// 將字符串進行 unicode 編碼  
    /// </summary>  
    /// <param name="str"></param>  
    /// <returns></returns>  
    public static string UnicodeEncode(this string str)  
    {  
        if (string.IsNullOrWhiteSpace(str))  
            return str;  
        StringBuilder strResult = new StringBuilder();  
        if (!string.IsNullOrEmpty(str))  
        {  
            for (int i = 0; i < str.Length; i++)  
            {  
                strResult.Append("\\u");  
                strResult.Append(((int)str[i]).ToString("x4"));  
            }  
        }  
        return strResult.ToString();  
    }  
    #endregion  
}  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM