0

I get a JSON object returned to me as follows:

result: {
 image: "..."
 title: "text text \"text\""
}

I am using underscore.js to render the template however when when it displays the title it includes the \" in the text.

e.g text text \"text\"

How can I unescape the the double quotes before displaying?

Thanks

Julian
  • 4,176
  • 19
  • 40
keshav
  • 866
  • 11
  • 19
  • 5
    That's not a real representation of the JSON object. The actual JSON must have `text text \\\"text\\\"` to produce the behavior you describe. The problem is that it's being escaped twice, and only (and correctly) unescaped once. Figure out where the second escaping is coming from. – user229044 Mar 03 '15 at 18:47
  • 2
    That isn't a JSON object though. JSON is just text. They're aren't literals in JSON. There are literals in JavaScript Objects, but not JSON. – Ryan Mar 03 '15 at 18:48
  • This is certainly not JSON. How are you retrieving the representation you posted? How is the data generated in the first place? If the output is `\"` then it means the string literally contains a \ . The proper way to fix that would be to fix the data generation/storage. – Felix Kling Mar 03 '15 at 18:50
  • The JSON in the question is whats being returned to the browser. It might be possible that the string is being doubled escaped. Currently investigating that possibility. Thanks for the info all! – keshav Mar 03 '15 at 19:01
  • something like: http://stackoverflow.com/questions/24559625/javascript-escape-double-quotes – Zeeshan Hassan Memon Mar 03 '15 at 19:06
  • It look like the string was being double escaped. @meagar if you want to post your comment as an answer i will accept it. Thanks all! – keshav Mar 03 '15 at 22:50

2 Answers2

0

.replace can do it:

text = result.text.replace(/\\([\\"])/g, '$1');
KyleK
  • 4,643
  • 17
  • 33
0

This link might be helpful, but I see in your comments that you are getting exactly same data as you have mentioned, so:

var result = { image: "...", title: "text text \"text\"" }

alert (result.title.toString().replace(/"/g, ''));
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Zeeshan Hassan Memon
  • 8,105
  • 4
  • 43
  • 57