I am using JSON.stringify
to create the following:
{"id":"3172043","Img":"../assets/7/14/NPCgunnerStanding.png","ImgWidth":145,"ImgHeight":210,"Width":"145","Height":"210","Left":649,"Top":165,"Parent":"3172015","MouseOver":"label({HTML: "Gunner"})","Idle":"animate({ rate:100 , pause:4000 })topImage("../assets/img/npc-exclamation3.gif")","MouseOut":"label({})","Proximity":"","Click":"modal({title:"Gunner",iframe: "../assets/7/modals/BEFGunner.php"})"}
and storing it in my db. This part is working fine, but when I look it up again I'm getting errors for the object because of the quotes inside of parentheses.
How can I escape just these quotes?
I started to put together a replacer function for the stringify
call that would check for these, but it started to seem like it would be messy and I was hoping to come up with a way to do it all at once with regex or something.
Please Assist :)
You need to escape at least
"
-> \\"
\\\\
-> \\\\
\\r
\\n
To do that in JS try this
var charToJson = { '"': '\\"', '\\': '\\\\', '\r': '\\r', '\n': '\\n' };
var JSONStringLiteral = '"'
+ myPlainTextString.replace(/[\\\"\r\n]/g, function (c) { return charToJson[c]; })
+ '"';
Alternatively, if you're running on a modern browser, JSON.stringify
should do this for you.
JSON.stringify('{"foo":"bar"}') === '"{\\"foo\\":\\"bar\\"}"'
Escape the double Quotes..
"label({HTML: \"Gunner\"})"
OR use single Quotes inside
"label({HTML: 'Gunner'})"
Use \\
like so:
{"key":"text (\"blabla\")"}
Or for cleaner code:
{'key':'text ("blabla")'}
But you have not choice if you have both "
and '
, like:
{"key":"text 'blabla' \"otherblabla\""}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.