码迷,mamicode.com
首页 > Windows程序 > 详细

json中含有Unicode的处理办法 C#

时间:2018-03-21 11:50:58      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:字符串   www.   http   post   this   body   encode   字符   esc   

原文: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  
}  

 

json中含有Unicode的处理办法 C#

标签:字符串   www.   http   post   this   body   encode   字符   esc   

原文地址:https://www.cnblogs.com/lonelyxmas/p/8615027.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!