返回json中含有......登陆

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

巴扎黑2016-11-10 14:45:04634

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

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  

}

最新手记推荐

• 用composer安装thinkphp框架的步骤• 省市区接口说明• 用thinkphp,后台新增栏目• 管理员添加编辑删除• 管理员添加编辑删除

全部回复(0)我要回复

暂无评论~
  • 取消回复发送