2

I am using the below line in Javascript to parse a json string.

var obj = JSON.parse('{"respDataMap":{"userMessages":{"lbSearchHint":"Enteravalueandpress\"Enter\"orclickon\"Search\"#"}},"respErrorCode":"","respErrorMessage":""}');

The escaped double-quote character in the string is causing the json parsing to fail. But, the same string pasted in online JSON validators is certified as valid. How do I fix this?

Teddy
  • 4,009
  • 2
  • 33
  • 55
  • At the time of asking this, I thought double backslash also didn't work. (This was because i was trying to replace single slash with a double slash in Java. Unfortunately my Java replaceAll usage was wrong leading me to think double backslash wasn't working in JS) – Teddy Mar 01 '14 at 18:26

1 Answers1

6

Use double slash like for escape character

var obj = JSON.parse('{"respDataMap":{"userMessages":{"lbSearchHint":"Enteravalueandpress\\"Enter\\"orclickon\\"Search\\"#"}},"respErrorCode":"","respErrorMessage":""}');
Sumeet Kumar Yadav
  • 11,912
  • 6
  • 43
  • 80
  • Thanks. Triple-backslash is also working.. why's that? – Teddy Feb 26 '14 at 13:05
  • Javascript interpreter will interprate three as as combination of double '\\' and single '\' which is interpreted as one '\'. '\\\' == '\\'+'\' == '\\' == '\'@Teddy And double slash also interpreter to single salsh ‘\\’ == ‘\’ – Sumeet Kumar Yadav Feb 26 '14 at 13:37