function decode($str) { //$lib='native'; // $lib='services_JSON'; // $lib='jsonrpc'; // dirty trick to correct bad json for photos //$str = preg_replace('/\t} {/','}, {', $str); // remove trailing , //$str = preg_replace('/,[ \r\n\t]+}/',' }', $str); // echo "Using $lib<BR>"; // echo $str; if (function_exists('json_decode') && JSON_LIB_DECODE == 'native') { $arr = json_decode($str, true); } else { if (JSON_LIB_DECODE == 'services_JSON') { require_once dirname(__FILE__) . '/json.php'; $json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE); $arr = $json->decode($str); } else { if (JSON_LIB_DECODE == 'jsonrpc') { require_once dirname(__FILE__) . '/jsonrpc/xmlrpc.php'; require_once dirname(__FILE__) . '/jsonrpc/jsonrpc.php'; $GLOBALS['xmlrpc_internalencoding'] = 'UTF-8'; //require_once dirname(__FILE__).'/jsonrpc/json_extension_api.php'; //$arr=json_decode($str, true); $value = php_jsonrpc_decode_json($str); if ($value) { $arr = php_jsonrpc_decode($value, array()); } } } } // print_r($arr); return $arr; }
/** * Takes a json-formetted string and decodes it into php values * @param string $json * @param bool $assoc * @return mixed * * @todo add support for assoc=false */ function json_decode($json, $assoc = false) { $jsval = php_jsonrpc_decode_json($json); if (!$jsval || $jsval->mytype != 3 && $jsval->mytype != 2) { return NULL; } else { $options = $assoc ? array() : array('decode_php_objs'); $val = php_jsonrpc_decode($jsval, $options); return $val; } }
/** * Takes a json-formetted string and decodes it into php values * @param string $json * @param bool $assoc * @return mixed * * @todo add support for assoc=false */ function json_decode($json, $assoc = false) { $jsval = php_jsonrpc_decode_json($json); if (!$jsval) { return NULL; } else { // up to php version 5.2.1, json_decode only accepted structs and arrays as top-level elements if ($GLOBALS['json_extension_api_120_behaviour'] && ($jsval->mytype != 3 && $jsval->mytype != 2)) { return NULL; } $options = $assoc ? array() : array('decode_php_objs'); $val = php_jsonrpc_decode($jsval, $options); return $val; } }