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

  :


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM