Json.Parse escape newline characters

≡放荡痞女 提交于 2019-12-04 05:32:46

You should just escape the \ as in \\n, your JSON becoming :

'[{"Name":"Eggs","Complete":false,"Notes":"Notes here\\n"},{"Name":"Sugar","Complete":false,"Notes":null}]';

If you cannot have access to the JSON, then your function should be :

function escapeSpecialChars(jsonString) {

    return jsonString.replace(/\n/g, "\\n")
        .replace(/\r/g, "\\r")
        .replace(/\t/g, "\\t")
        .replace(/\f/g, "\\f");

}

 var groceriesJson = JSON.parse(escapeSpecialChars(jsonString));

As @Quentin suggests you can skip storing the value inside the literal and simply do something like this:

var jsonObject = @Html.Raw(groceries);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!