考虑我有以下字符串:
{
"{\n <<<-- error
\"SomeKey\": {\n \"somevalue\": \"test\",\n,
\"AnotherKey\": \"Long string should be here \n another line break here \n and another line here \"
}
}
当您尝试使用 JSON.parse 解析此字符串时,它会抛出指向第一个换行符的错误。有什么方法可以在不删除不在引号内的\n 的情况下消除换行符。
最佳答案
从 JSON 字符串中剥离 \n
并执行 JSON.parse
var json_data = "{\n \"Fullname\": \"Alex Johnson\",\n \"FirstName\": \"Alex\", \n \"LastName\": \"Johnson\"\n }";
var obj = JSON.parse(json_data.replace(/\r?\n|\r/g, ''));
console.log(obj);
关于javascript - 从 JSON 中删除新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45208252/