Parsing JSON; back-slash problem
Parsing JSON; back-slash problem
am 15.12.2009 12:52:17 von Andrew Burgess
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\",\"lna me\":\"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?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Parsing JSON; back-slash problem
am 15.12.2009 13:06:24 von Ashley Sheridan
--=-YaSDfbdABfI5D2nISsoA
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
On Tue, 2009-12-15 at 06:52 -0500, Andrew Burgess wrote:
> 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\",\"lna me\":\"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?
>
Turn off magic quotes, as it looks like they are enabled on your server.
You can turn them off from the .htaccess file if you don't have access
to the php.ini
Thanks,
Ash
http://www.ashleysheridan.co.uk
--=-YaSDfbdABfI5D2nISsoA--
Re: Parsing JSON; back-slash problem
am 15.12.2009 15:43:45 von Shawn McKenzie
Ashley Sheridan wrote:
> On Tue, 2009-12-15 at 06:52 -0500, Andrew Burgess wrote:
>
>> 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\",\"lna me\":\"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?
>>
>
>
> Turn off magic quotes, as it looks like they are enabled on your server.
> You can turn them off from the .htaccess file if you don't have access
> to the php.ini
>
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
>
If you don't have access to do this, look at stripslashes()
--
Thanks!
-Shawn
http://www.spidean.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Parsing JSON; back-slash problem
am 15.12.2009 15:54:17 von public
--000e0cd17aae7275f6047ac59063
Content-Type: text/plain; charset=UTF-8
>
>
> If you don't have access to do this, look at stripslashes()
>
And if you absolutely want to be on the safe side - check* if the
magic_quotes option is enabled - if so; do stripslashes. If not - then
obviously don't.
* http://www.php.net/manual/en/function.get-magic-quotes-gpc.p hp
>
>
> --
> Thanks!
> -Shawn
> http://www.spidean.com
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--
http://www.interpotential.com
http://www.ilikealot.com
Phone: +4520371433
--000e0cd17aae7275f6047ac59063--
Re: Parsing JSON; back-slash problem
am 15.12.2009 17:21:25 von Andrew Burgess
Thanks guys; I've got it working now!
On Tue, Dec 15, 2009 at 9:54 AM, Wouter van Vliet / Interpotential
wrote:
>>
>> If you don't have access to do this, look at stripslashes()
>
> And if you absolutely want to be on the safe side - check* if the
> magic_quotes option is enabled - if so; do stripslashes. If not - then
> obviously don't.
> *Â http://www.php.net/manual/en/function.get-magic-quote s-gpc.php
>
>>
>> --
>> Thanks!
>> -Shawn
>> http://www.spidean.com
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>
>
>
> --
> http://www.interpotential.com
> http://www.ilikealot.com
>
> Phone: +4520371433
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php