开发者

Can't unescape JSON content in javascript

开发者 https://www.devze.com 2023-03-15 20:53 出处:网络
I\'m generating a JSON file using PHP\'s json_encode function, and I\'ve escaped double-quote characters with ", so the output line looks like this:

I'm generating a JSON file using PHP's json_encode function, and I've escaped double-quote characters with ", so the output line looks like this:

"apply_instructions":"<p>Visit <a href=&#34;http:\/\/www.google.com&#34;>www.google.com<\/a><\/p>"

I'm then using JQuery's getJSON function to retrieve and loop through the file. I'm attempting to unencode the &#34; using this:开发者_C百科

entry.apply_instructions = entry.apply_instructions.replace('&#34;', '"');

For whatever reason, it's not working. The first quote mark seems to be getting replaced, but the second one won't budge. I've tried using other random find-and-replace characters, with similar results.


.replace() only replaces the first instance, you need to use regex to do a global replace.

IE:

.replace(/&#34;/g, "\"");


Try not escaping the quotes (in php):

$arr = array( 
    "apply_instructions"=>'<p>Visit <a href="http://www.google.com">www.google.com</a></p>'
);

echo json_encode($arr); // it will do all the escaping for you

See demo: http://codepad.org/VzT99sqM


You can either use regex to replace all instances of &#34;.

Or use a different function to escape the quotes using backslashes.

0

精彩评论

暂无评论...
验证码 换一张
取 消