The = sign in the JSON text causes a parsing error

"Cost to Implement \nRate 5 to 1\nHigh = 5\nLow = 1" 

since part of JSON is well versed in jsonlint, but doesnโ€™t work in Chrome with any of these approaches (each time tried separately):

 sections = $.parseJSON(myJSONstr); sections = JSON.parse(myJSONstr); sections = eval('(' + myJSONstr + ')'); 

When I remove the = signs from a string in JSON, everything is fine. My users will have to enter the = sign in the text they enter. Is there any way around this?

+4
source share
1 answer

It looks like you are entering a new line without slipping away from it. You need to avoid backslashes.

The following errors occur because you enter a new line in a string in JSON, they must be escaped

 var obj = JSON.parse('{"prop": "Cost to Implement \nRate 5 to 1\nHigh = 5\nLow = 1"}'); 

Backslash Escape

 // Works fine var obj = JSON.parse('{"prop": "Cost to Implement \\nRate 5 to 1\\nHigh = 5\\nLow = 1"}'); 

Note that these newlines (and other characters that should be escaped as tabs, backspaces ...) will be escaped automatically if you arrange your JSON objects correctly. for instance

 // Correctly parses the new line JSON.parse(JSON.stringify({prop: "Line1\nLine2\tAfterTab"})) 
+6
source

Source: https://habr.com/ru/post/1447033/


All Articles