a note on json_de/encode

I just thought I should quickly post this note simply because I know a few people might have problems with parsing it with the above functions.

I would like first to say that json_encode and json_decode are PHP 5.2.0+ methods of parsing and dealing with Javascript Simple Object Notation. basically looks a bit like this {“key”:”value”}. The idea is to be able to do useful things with objects like these and stepping through the key and value pairs while doing something with them.

The problem however is you sometimes get JSON sent as a string through an HTTP method like $_POST and the quotes in there are automatically (well in my case). escaped with backslashes like this {\”jsonkey\”:\”jsonvalue\”}. Before you actually call json_decode on your json data use the built in stripslashes function first. so your code should look like this in one line

$json_array = json_decode(stripslashes($jsonObject));

hope this helps someone.

About Kwabena Aning

Website and Information Systems Manager living in London with an avid interest in web services, games, web development, Linux, chess, MVC Design patterns, Information Archetecture and most things geeky. visit www.kaning.co.uk for my portfolio and other bits of information.
This entry was posted in Web Development, jquery, php and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>