I’m connecting to a PHP webservice that returns URLs of images in JSON (using the PHP json_encode function).
I’m working within a mootools 1.2.5 system, which also needs to work on mobile phones.
The format of the image node returned in the data is like this:
"thumb": "<img src=\"http://photos.imgserv.com/201107222000000.jpg\" />"
For some reason iphone and android see a NULL value when I pass the response.JSON to them. I can pass the response.text object to them with no problem, but then the JSON.decode fails since the double quotes are not escaped properly. If I manually add double back slashes on the image tags like this:
"thumb": "<img src=\\"http://photos.imgserv.com/201107222000000.jpg\\" />"
it all works as designed. However, I’m having a hell of a time getting the right regexp to replace the \" in the original response.text with \\".
Is there a “right” way to handle the response.JSON to mobile phones, and alternately how can I properly write the string.replace() regex to handle these escape chars?
Thanks!
EDIT TO ADD:
Here’s two jsfiddles with the single and double backslash to show the issue:
Single backslash (doesn’t decode/parse properly) — http://jsfiddle.net/Qde6F/
Double backslash (does decode/parse properly) — http://jsfiddle.net/Qde6F/1/
did you try something like this…
ie, the replace expression is
replace("\\\"","\\\\\""), which replace\"with\\"Ok, this worked for me in the jsfiddler
Basically the regex rewrites the
src="url"to besrc=\"url\".