解決json object轉string,value值存在特殊符號,無法解析問題

昨天在JSON.stringify()轉數組的時候,發現一直報錯,最終確定原因爲string中的空格在html顯示的時候,會自動加上 
知道了問題所在,下面講解如何解決問題。我們在取數據時,用HTMLDecode2()方法過濾下特殊字符即可

function HTMLDecode2(str)
{
    if (str.length === 0)
        return "";

    var result = "" + str;
    result = result.replace(">", ">");
    result = result.replace("&lt;", "<");
    result = result.replace("&nbsp;", " ");
    result = result.replace("&quot;", "\"");
    result = result.replace("&#39;", "\'");
    //對斜線的轉義  
    result = result.replace("\\\\", "\\");
    //注意php中替換的時候只能用雙引號"\n"
    result = result.replace("\\n", "\n");  
    result = result.replace("\\r", "\r");

    return result;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章