JSON解析值富文本

解析前端传递的JSON数据中可能如下

{ "result": "<input value="Test" type="button" onclick="alert(""OK"");" />", "msg": "test"} 

此时去解析是无法解析出来的,存在 / 空格 多的双引号,

参考多个结果

针对双引号(利用中文双引号代替多余的英文双引号后去解析JSON串)

public String jsonStringConvert(String s) {
        char[] temp = s.toCharArray();
        int n = temp.length;
        for (int i = 0; i < n; i++) {
            if (temp[i] == ':' && temp[i + 1] == '"') {
                for (int j = i + 2; j < n; j++) {
                    if (temp[j] == '"') {
                        if (temp[j + 1] != ',' && temp[j + 1] != '}') {
                            temp[j] = '”';
                        } else if (temp[j + 1] == ',' || temp[j + 1] == '}') {
                            break;
                        }
                    }
                }
            }
        }
        return new String(temp);
    }

针对空格(先调用此方法)

public String replaceBlank(String str) {
        String dest = "";
        if (str != null) {

            Pattern p = Pattern.compile("\\s*|\t|\r|\n");
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
            // Pattern p2 = Pattern.compile("\\s*\"");
            // Matcher m2 = p2.matcher(dest);
            // dest = m2.replaceAll("\'");
            dest = dest.replace("=\"", "='");
            p = Pattern.compile("\"\0*>");
            m = p.matcher(dest);
            dest = m.replaceAll(">'");
        }
        return dest;

    }

以上会造成数据的格式少了空格,需要自己去添加上(不能很好的解决问题)

对双引号进行转译

对富文本加密,后台解密存储

 

 

 

 

参考:https://blog.csdn.net/jbb0403/article/details/45918693

  :

posted @ 2018-11-13 18:23  TracyRanch  阅读(2431)  评论(0编辑  收藏  举报