java 将含有unicod的字符串 转成中文

注:此段代码为从网上转载,原著找不到了,找到的也是转载文章,我在此作为收藏!虽然不知道原著是谁,但仍表示感谢!

private static String ascii2native ( String asciicode )
    {
        String[] asciis = asciicode.split ("\\\\u");
        String nativeValue = asciis[0];
        try
        {
            for ( int i = 1; i < asciis.length; i++ )
            {
                String code = asciis[i];
                nativeValue += (char) Integer.parseInt (code.substring (0, 4), 16);
                if (code.length () > 4)
                {
                    nativeValue += code.substring (4, code.length ());
                }
            }
        }
        catch (NumberFormatException e)
        {
            return asciicode;
        }
        return nativeValue;
    }

    public static void main ( String[] args )
    {
        String str = "\"JWHQK_JWQC\":\"\u6c5f\u5b89\u793e\u533a\u5c45\u6c11\u59d4\u5458\u4f1a\"";
        String result = ascii2native (str);
        System.out.println(result);
    }

你可能感兴趣的:(java)