JSON.parse解析遇到特殊字符会报错,特殊字符包括<、>、“”、\n等等
function jsonString(str) {
if (str != null && str != '') {
str = str.replace(/\\/g, "\\\\");
str = str.replace(/\n/g, "\\n");
str = str.replace(/\r/g, "\\r");
str = str.replace(/\t/g, "\\t");
str = str.replace(/("")+/g, "\"\"");
str = str.replace(/\'/g, "'");
str = str.replace(/ /g, " ");
str = str.replace(/</g, "<");
str = str.replace(/>/g, ">");
}
return str
}
网友评论