5

Possible Duplicate:
How can I decode json in PHP 5.1?

I am using json_encode function it works fine in my localhost...when i moved to server it not working ...I googled and find out that its does not support 5.1 version ...I want to use this function .any other possibility?wheather i need to upgrade to 5.2 or wat?

4
  • Please see this thread stackoverflow.com/questions/2370968/… Commented Jul 27, 2012 at 8:49
  • json_encode works under 5.1 too (see de2.php.net/manual/en/function.json-encode.php PHP 5 >= 5.2.0, PECL json >= 1.2.0). I would tipp: your don't have pecl/json installed on your server Commented Jul 27, 2012 at 8:53
  • Do you know how old PHP 5.1 is? Apart from being slow and lacking many very important features, it also has known security vulnerabilities. Upgrade to PHP 5.3 or PHP 5.4. Commented Jul 27, 2012 at 9:00
  • 1
    Thnks guys i found my answer ....boutell.com/scripts/jsonwrapper.html Commented Jul 29, 2012 at 8:05

3 Answers 3

17

Here's what I've used successfully for php 5.1 (Taken from comments under http://www.php.net/json_encode):

/**
 * Supplementary json_encode in case php version is < 5.2 (taken from http://gr.php.net/json_encode)
 */
if (!function_exists('json_encode'))
{
    function json_encode($a=false)
    {
        if (is_null($a)) return 'null';
        if ($a === false) return 'false';
        if ($a === true) return 'true';
        if (is_scalar($a))
        {
            if (is_float($a))
            {
                // Always use "." for floats.
                return floatval(str_replace(",", ".", strval($a)));
            }

            if (is_string($a))
            {
                static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'));
                return '"' . str_replace($jsonReplaces[0], $jsonReplaces[1], $a) . '"';
            }
            else
            return $a;
        }
        $isList = true;
        for ($i = 0, reset($a); $i < count($a); $i++, next($a))
        {
            if (key($a) !== $i)
            {
                $isList = false;
                break;
            }
        }
        $result = array();
        if ($isList)
        {
            foreach ($a as $v) $result[] = json_encode($v);
            return '[' . join(',', $result) . ']';
        }
        else
        {
            foreach ($a as $k => $v) $result[] = json_encode($k).':'.json_encode($v);
            return '{' . join(',', $result) . '}';
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

will it work perfect perikiles...?
Well I've used it a lot and for many years without a problem, but I can't tell 100% for sure. Upgrading to php 5.2+ is the preferred solution
2

Yes json_encode is available in PHP 5 >= 5.2.0, you must either upgrade (recomended) or find a library that implements the functionality.

Comments

2

Take a look at http://de.php.net/json_encode in the comments. Some people provided a PHP-written function which does the same. Only the performance will (most likely) not be as good as the native one ;-).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.