I have a page where I am trying to parse following json string using JSON.parse
'[{"Name":"Eggs","Complete":false,"Notes":"Notes here\n"},{"Name":"Sugar","Complete":false,"Notes":null}]'
But following code gives error "Uncaught SyntaxError: Unexpected token"
var groceriesJson = JSON.parse(jsonString);
Then I came to know that its because of \n
in json string. So I did try this solution. But no luck. Still same error "Uncaught SyntaxError: Unexpected token"
function escapeSpecialChars(jsonString) {
return jsonString.replace(/\\n/g, "\\n")
.replace(/\\'/g, "\\'")
.replace(/\\"/g, '\\"')
.replace(/\\&/g, "\\&")
.replace(/\\r/g, "\\r")
.replace(/\\t/g, "\\t")
.replace(/\\b/g, "\\b")
.replace(/\\f/g, "\\f");
}
var groceriesJson = JSON.parse(escapeSpecialChars(jsonString));
Any ideas? Thanks
---UPDATE----
I am not creating this string manually, I have c# codes that creates json string from c# objects
var jss = new System.Web.Script.Serialization.JavaScriptSerializer();
var groceries = jss.Serialize(Model);
then in javascript codes I have
var jsonString = '@Html.Raw(groceries)'
var groceriesJson = JSON.parse(escapeSpecialChars(jsonString));
I have a page where I am trying to parse following json string using JSON.parse
'[{"Name":"Eggs","Complete":false,"Notes":"Notes here\n"},{"Name":"Sugar","Complete":false,"Notes":null}]'
But following code gives error "Uncaught SyntaxError: Unexpected token"
var groceriesJson = JSON.parse(jsonString);
Then I came to know that its because of \n
in json string. So I did try this solution. But no luck. Still same error "Uncaught SyntaxError: Unexpected token"
function escapeSpecialChars(jsonString) {
return jsonString.replace(/\\n/g, "\\n")
.replace(/\\'/g, "\\'")
.replace(/\\"/g, '\\"')
.replace(/\\&/g, "\\&")
.replace(/\\r/g, "\\r")
.replace(/\\t/g, "\\t")
.replace(/\\b/g, "\\b")
.replace(/\\f/g, "\\f");
}
var groceriesJson = JSON.parse(escapeSpecialChars(jsonString));
Any ideas? Thanks
---UPDATE----
I am not creating this string manually, I have c# codes that creates json string from c# objects
var jss = new System.Web.Script.Serialization.JavaScriptSerializer();
var groceries = jss.Serialize(Model);
then in javascript codes I have
var jsonString = '@Html.Raw(groceries)'
var groceriesJson = JSON.parse(escapeSpecialChars(jsonString));
Share
Improve this question
edited May 23, 2017 at 12:09
CommunityBot
11 silver badge
asked Oct 17, 2014 at 8:28
sanjeevsanjeev
1,4072 gold badges18 silver badges30 bronze badges
2
- 3 Why are you trying to embed JSON as a string literal in your JavaScript? Just use it as an array literal and skip the manual parsing step. – Quentin Commented Oct 17, 2014 at 8:32
- That works.I'll mark it as answer if you can post answer. Thanks – sanjeev Commented Oct 17, 2014 at 8:43
2 Answers
Reset to default 7You should just escape the \
as in \\n
, your JSON being :
'[{"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);