[SOLVED] JSON to array or serialize

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

[SOLVED] JSON to array or serialize

Post 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
Last edited by anjanesh on Fri Mar 09, 2007 11:27 am, edited 1 time in total.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

There's a json extension available, http://de2.php.net/json
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
Last edited by volka on Fri Mar 09, 2007 10:43 am, edited 1 time in total.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post 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 !
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post 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.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post 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.
Post Reply