關於web傳給後臺的json中包含特殊字元導致無法解析的解決方案
阿新 • • 發佈:2019-02-05
最近做的專案中涉及到大量英文,web傳json給後臺時會存在英文的 ""(雙引號):(冒號),(逗號)
會出現json解析失敗,因此需要web將這些特殊的字元進行轉換:
stuJson=stuJson.replaceAll("\n", "<br\\>");
存入資料庫時回車替換為<br\\>,
返回給web端時再從SQL語句中替換回去:IFNULL(REPLACE( b.interview_remark,'<br\>','\n'),'') interview_remark
目前運用到了這些,還有更好的方法歡迎大家指導指導
會出現json解析失敗,因此需要web將這些特殊的字元進行轉換:
var txt=$(".tInfo").eq(i).val().replace(/\"/g,"'");
var txt2=txt.replace(/\,/g,",")
//console.log(txt2)
// var txt2=txt.replaceAll(",",",");
brr.push(txt2);
在web端組json前進行轉換,以此來避開json解析異常的情況。
還有個情況就是web端傳入回車時,解析也會出錯,因此我在後臺json解析前 進行了替換:
stuJson=stuJson.replaceAll("\n", "<br\\>");
存入資料庫時回車替換為<br\\>,
返回給web端時再從SQL語句中替換回去:IFNULL(REPLACE( b.interview_remark,'<br\>','\n'),'') interview_remark
目前運用到了這些,還有更好的方法歡迎大家指導指導