PHP Json Encoding w/ quote escaping in 5.2?

Posted by NickAldwin on Stack Overflow See other posts from Stack Overflow or by NickAldwin
Published on 2010-03-11T17:55:20Z Indexed on 2010/03/11 17:59 UTC
Read the original article Hit count: 328

Filed under:
|
|
|
|

I'm playing with the flickr api and php. I want to pass some information from PHP to Javascript through Ajax. I have the following code:

json_encode($pics);

which results in the following example JSON string:

[{"id":"4363603591","title":"blue, white and red...another seattle view","date_faved":"1266379499"},{"id":"4004908219","title":"\u201cI just told you my dreams and you made me see that I could walk into the sun and I could still be me and now I can't deny nothing lasts forever.\u201d","date_faved":"1259987670"}]

Javascript has problems with this, however, due to the unescaped single-quote in the second item ("can't deny").

I want to use the function json_encode with the options parameter to make it strip the quotes, but that's only available in PHP 5.3, and I'm running 5.2 (not my server). Is there a fast way to run through the entire array and escape everything before encoding it in Json? I looked for a way to do this, but it all seems to deal with encoding it as the data is generated, something I cannot do as I'm not the one generating the data.

If it helps, I'm currently using the following javascript after the ajax request:

var photos = eval('(' + resptxt + ')');  

© Stack Overflow or respective owner

Related posts about JSON

Related posts about JavaScript