[SOLVED] JSON to array or serialize
Moderator: General Moderators
[SOLVED] JSON to array or serialize
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
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
Last edited by anjanesh on Fri Mar 09, 2007 11:27 am, edited 1 time in total.
There's a json extension available, http://de2.php.net/json
- Buddha443556
- Forum Regular
- Posts: 873
- Joined: Fri Mar 19, 2004 1:51 pm
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.
http://mike.teczno.com/JSON/JSON.phps
That has a JSON decoder but I'm not sure it's what your looking for though.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
It should be noted, only as of PHP 5.2, althought the manual does provide links to pure php solutions.volka wrote:There's a json extension available, http://de2.php.net/json
- Maugrim_The_Reaper
- DevNet Master
- Posts: 2704
- Joined: Tue Nov 02, 2004 5:43 am
- Location: Ireland
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.
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.
But if you follow the link to the pecl repository you will find it's available for all versions >= 4.3.0http://de2.php.net/json wrote:As of PHP 5.2.0, the JSON extension is bundled and compiled into PHP by default.
Last edited by volka on Fri Mar 09, 2007 10:43 am, edited 1 time in total.
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.
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.