Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I decode json in PHP 5.1?

Tags:

json

php

parsing

json_decode function is not part of PHP 5.1, so I cannot use it. Is there any other function for this version?

like image 273
newbie Avatar asked Mar 03 '10 12:03

newbie


People also ask

What is JSON decode in PHP?

The json_decode() function is used to decode or convert a JSON object to a PHP object.

What json_decode () function will return?

The json_decode() function can return a value encoded in JSON in appropriate PHP type. The values true, false, and null is returned as TRUE, FALSE, and NULL respectively. The NULL is returned if JSON can't be decoded or if the encoded data is deeper than the recursion limit.

Does JSON decode return an array?

Does JSON decode return an array? The json_decode() function returns an object by default. The json_decode() function has a second parameter, and when set to true, JSON objects are decoded into associative arrays.


2 Answers

Before PHP 5.2, you can use the JSON PECL extension.

In fact, it is the extension that has been integrated into PHP 5.2 (quoting) :

As of PHP 5.2.0, the JSON extension is bundled and compiled into PHP by default.


Some other solutions would be to use some component developped in PHP.

Some time ago (about one year ago), I used the Zend_Json component of Zend Framework, with PHP 5.1.

Even if Zend Framework requires PHP 5.2, that component can be extracted (I think it depends only on one other component -- maybe Zend_Exception, or something like that) -- and one year ago, it was possible to use it with PHP 5.1.


The official JSON website also links to several PHP-based or PHP-oriented components -- you might want to take a look at that list.

like image 81
Pascal MARTIN Avatar answered Sep 28 '22 11:09

Pascal MARTIN


I ran into the same issue running PHP 5.1.6, but I couldn't upgrade or install extensions on my client's server. To make matters worse, the JSON.org site was down when I needed a solution but fortunately this file on Google Code worked perfectly! I would have preferred to actually define json_encode/json_decode, but calling fromJSON() worked just fine.

http://code.google.com/p/simplejson-php/

like image 27
Marcy Sutton Avatar answered Sep 28 '22 11:09

Marcy Sutton