美文网首页
JSON.parsr报错Unexpected token

JSON.parsr报错Unexpected token

作者: 毛毛_000e | 来源:发表于2019-11-14 14:54 被阅读0次

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, "&#39;");

                str = str.replace(/ /g, "&nbsp;");

                str = str.replace(/</g, "&lt;");

                str = str.replace(/>/g, "&gt;");

            }

            return str

        }

相关文章

网友评论

      本文标题:JSON.parsr报错Unexpected token

      本文链接:https://www.haomeiwen.com/subject/boerictx.html