前言:
json格式数据中是不能有换行符“\n”的,有的话解析时会抛出异常
正文:
JS:
var myJSONString = JSON.stringify(myJSON); var myEscapedJSONString = myJSONString.replace(/\\n/g,"\\n") .replace(/\\‘/g,"\\‘") .replace(/\\"/g,"\\\"") .replace(/\\&/g,"\\&") .replace(/\\r/g,"\\r") .replace(/\\t/g,"\\t") .replace(/\\b/g,"\\b")
Java:
array.replaceAll("\n","\\\\n")
参考博客:
Json处理换行符 - dchjmichael的专栏 - CSDN博客
https://blog.csdn.net/dchjmichael/article/details/8243199
探究java中String.replaceAll方法把换行符(\n)替换为明文显示(\n)为何需要四个反斜杠(\\\\n) - 禤永豪 - CSDN博客
https://blog.csdn.net/xuanyonghao/article/details/75053469