Page 1 of 1

[SOLVED] JSON to array or serialize

Posted: Fri Mar 09, 2007 10:21 am
by anjanesh
Hi

Is there any direct (common) method to convert a json string to a php array or serialize format ?

Im fetching a remote html content which has JavaScript code in it and it has some JSON varialbes defined. Is there some easy way to convert that to a php array-variable ?

Thanks

Posted: Fri Mar 09, 2007 10:25 am
by infolock
Had to write my own method. It's not the hardest thing in the world, but there is not a built-in php function that i know of if that is what you are asking.

Posted: Fri Mar 09, 2007 10:29 am
by volka
There's a json extension available, http://de2.php.net/json

Posted: Fri Mar 09, 2007 10:36 am
by Buddha443556
PEAR class ...

http://mike.teczno.com/JSON/JSON.phps

That has a JSON decoder but I'm not sure it's what your looking for though.

Posted: Fri Mar 09, 2007 10:37 am
by John Cartwright
volka wrote:There's a json extension available, http://de2.php.net/json
It should be noted, only as of PHP 5.2, althought the manual does provide links to pure php solutions.

Posted: Fri Mar 09, 2007 10:37 am
by Maugrim_The_Reaper
PHP 5.2 has json_decode(). The Zend Framework also has a Zend_Json class which does not rely on (but will use if available) json_decode() and json_encode(). The ZF class is likely workable down to 5.1.0. PEAR's Services_JSON works in PHP4 AFAIK.

Be aware that as of PHP5.2.1 the ext/json extension (enabled by default) has a few differences which some might call "bugs", since it it fails the JSON Checker published by json.org. Not a clue if this is to be "fixed" or left as is since it was intentionally changed by the internals team. In any case, this is unlikely to effect you when using arrays/objects - it's mainly an issue with handling boolean/null/string usage.

Posted: Fri Mar 09, 2007 10:41 am
by volka
http://de2.php.net/json wrote:As of PHP 5.2.0, the JSON extension is bundled and compiled into PHP by default.
But if you follow the link to the pecl repository you will find it's available for all versions >= 4.3.0

Posted: Fri Mar 09, 2007 10:43 am
by anjanesh
Wow ! JSON extension built-in by default in PHP 5.2.0 ! And Im sticking with 5.1.2 thinking theres nothing I can possibly do with the newer versions.
And to my surprise, one my shared accounts is currently using PHP 5.2.0!

Thank a lot !

Posted: Fri Mar 09, 2007 11:39 am
by infolock
Yeah, I guess I should have noted that I'm one of the guys who is still in the php 4 era and who doesn't use pear because of overhead (=

Otherwise, yep, they are absolutely right.

Posted: Fri Mar 09, 2007 1:03 pm
by anjanesh
Well...since most of the shared hosts are now moving to PHP5, we can make use of the version 5 features.

For those who end up searching : One of my sites is hosted on a shared account at Padberg-IT - and my phpinfo shows PHP 5.2.0, MySQL 5.0.30, PostgreSQL 7.4.7, mod_perl and mod_python !

EDIT:
The function json_decode() needs more work to be done on.
Beware when decoding JSON from JavaScript. Almost nobody uses quotes for object property names and none of the major browsers require it, but this function does! {a:1} will decode as NULL, whereas the ugly {"a":1} will decode correctly.