Json特殊字符处理代码

 /**
     * 去除会带来json格式出错的特殊字符.
     *
     * @param string
     * @return
     */
    public static String replaceSpecialStr(String string) {
        if (string == null || string.length() == 0) {
            return "";
        }

        char b;
        char c = 0;
        int i;
        int len = string.length();
        StringBuffer sb = new StringBuffer(len + 4);
        String t;

        for (i = 0; i < len; i += 1) {
            b = c;
            c = string.charAt(i);
            switch (c) {
                case '\\':
                    break;
                case '\'':
                    break;
                case '"':
                    break;
                case '/':
                    break;
                case '\b':
                    break;
                case '\t':
                    break;
                case '\n':
                    break;
                case '\f':
                    break;
                case '\r':
                    break;
                default:
                    if (c < ' ') {
                    } else {
                        sb.append(c);
                    }
            }
        }
        return sb.toString();
    }
 

 

你可能感兴趣的:(json)