Parsing JSON; back-slash problem
| From: | Andrew Burgess | Date: | Tue, 15 Dec 2009 11:52:17 +0000 |
| Subject: | Parsing JSON; back-slash problem | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-300515@lists.php.net to get a copy of this message | ||
This seems like a pretty basic question, but it has me stumped.
Here's my scenario: I'm using Douglas Crockford's JSON2.js to parse an
object in JavaScript, which I then pass to a PHP script to store in a
file. I use JSON.stringify() on the object, which logs to the console
as this:
{"employees":{"data":{"John":{"fname":"John","lname":"Doe","city":"Toronto","country":"Canada"}}}}
Then I use the jQuery POST function to send it to a PHP script. Before
doing anything with it in PHP, I log the received value to the
console, and this is what I get:
{\"employees\":{\"data\":{\"John\":{\"fname\":\"John\",\"lname\":\"Doe\",\"city\":\"Toronto\",\"country\":\"Canada\"}}}}
The problem is, when I call the script to retrieve this data from a
file, JSON.parse can't parse it because of the back-slashes. I'm
pretty sure my problem is on the PHP side (since it's fine coming out
of JS); what do I need to do to fix this? is a preg_replace enough?