对于老的php版本是没有json_encode 和json_decode函数的,但是又想使用这个功能。下边就是一个用php代码写的json encode 和decode 的函数。
function fromJSON ( $json, $assoc = false ) {
/* by default we don't tolerate ' as string delimiters
if you need this, then simply change the comments on
the following lines: */
$matchString = '/(".*?(?<!\\\\)"|\'.*?(?<!\\\\)\')/';
//$matchString = '/".*?(?<!\\\\)"/';
// safety / validity test
$t = preg_replace( $matchString, '', $json );
$t = preg_replace( '/[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/', '', $t );
if ($t != '') { return null; }
// build to/from hashes for all strings in the structure
$s2m = array();
$m2s = array();
preg_match_all( $matchString, $json, $m );
foreach ($m[0] as $s) {
$hash = '"' . md5( $s ) . '"';
$s2m[$s] = $hash;
$m2s[$hash] = str_replace( '$', '\$', $s ); // prevent $ magic
}
// hide the strings
$json = strtr( $json, $s2m );
// convert JS notation to PHP notation
$a = ($assoc) ? '' : '(object) ';
$json = strtr( $json,
array(
':' => '=>',
'[' => 'array(',
'{' => "{$a}array(",
']' => ')',
'}' => ')'
)
);
// remove leading zeros to prevent incorrect type casting
$json = preg_replace( '~([\s\(,>])(-?)0~', '$1$2', $json );
// return the strings
$json = strtr( $json, $m2s );
/* "eval" string and return results.
As there is no try statement in PHP4, the trick here
is to suppress any parser errors while a function is
built and then run the function if it got made. */
$f = @create_function( '', "return {$json};" );
$r = ($f) ? $f() : null;
// free mem (shouldn't really be needed, but it's polite)
unset( $s2m ); unset( $m2s ); unset( $f );
return $r;
}
/**
* Encodes a PHP variable into a JSON string.
* @param mixed $value A PHP variable to be encoded.
*/
function toJSON ( $value ) {
if ($value === null) { return 'null'; }; // gettype fails on null?
$out = '';
$esc = "\"\\/\n\r\t" . chr( 8 ) . chr( 12 ); // escaped chars
$l = '.'; // decimal point
switch ( gettype( $value ) )
{
case 'boolean':
$out .= $value ? 'true' : 'false';
break;
case 'float':
case 'double':
// PHP uses the decimal point of the current locale but JSON expects %x2E
$l = localeconv();
$l = $l['decimal_point'];
// fallthrough...
case 'integer':
$out .= str_replace( $l, '.', $value ); // what, no getlocale?
break;
case 'array':
// if array only has numeric keys, and is sequential... ?
for ($i = 0; ($i < count( $value ) && isset( $value[$i]) ); $i++);
if ($i === count($value)) {
// it's a "true" array... or close enough
$out .= '[' . implode(',', array_map('toJSON', $value)) . ']';
break;
}
// fallthrough to object for associative arrays...
case 'object':
$arr = is_object($value) ? get_object_vars($value) : $value;
$b = array();
foreach ($arr as $k => $v) {
$b[] = '"' . addcslashes($k, $esc) . '":' . toJSON($v);
}
$out .= '{' . implode( ',', $b ) . '}';
break;
default: // anything else is treated as a string
return '"' . addcslashes($value, $esc) . '"';
break;
}
return $out;
}
相关推荐
NULL 博文链接:https://onestopweb.iteye.com/blog/2380018
PHP json_encode与json_decode.rar
json_decode 索引为数字时自动排序问题解决方法.docx
见和系统要求 * PHP* mbstring extension* Charset UTF-8使用 php-json 如果您有错误“调用未定义的函数 json_encode() 或 json_decode()”,只需添加: require_once("phpJson.class.php");或者 require_once(...
用于 php json 编码/解码功能的 json 包装器 <?php use Json \ JsonArrayDecoder ; use Json \ Exception \ JsonException ; try { $ string = '{"name":"value"}' ; $ decoder = new JsonArrayDecoder (); $...
##json_decode_fixPHP的json_encode总有一些不通用的缺陷,例如不能解析js的json对象(当键名没有引号时)。json_decode_fix即可以解决部分bug,简单测试过string,integer,boolean等类型。继续测试中。
NULL 博文链接:https://shiren1118.iteye.com/blog/309232
php中json_decode()和json_encode()的使用方法.docx
本文实例讲述了PHP实现json_decode不转义中文的方法。分享给大家供大家参考,具体如下: 默认情况下PHP的 json_decode 方法会把特殊字符进行转义,还会把中文转为Unicode编码形式。 这使得数据库查看文本变得很麻烦...
一、json_encode() 对变量进行JSON编码 语法:json_encode($value[,$options=0]) 注意: 1、$value为要编码的值,且该函数只对UTF8编码的数据有效; 2、options:由以下常量组成的二进制掩码:JSON_HEX_QUOT, ...
下面小编就为大家带来一篇关于php中的json_encode()和json_decode()函数的一些说明。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
主要介绍了php json_encode与json_decode详解及实例的相关资料,需要的朋友可以参考下
据他所说,json_encode和json_decode比内置的serialize和unserialize函数要高效。 于是我决定动手实验,证实一下同事所说的情况是否属实。 实验分别在PHP 5.2.13和PHP 5.3.2环境下进行。 用同一个变量,分别用以上...
围绕json_encode()和json_decode()的简单包装,用于捕获任何错误而无需执行json_last_error() 。 在PHP 7.3中,这些函数将。 use Eastwest \ Json \ Json ; use Eastwest \ Json \ JsonException ; try { $ json =...
写接口的同学应该会经常遇到数据格式的转换,这时候必不可少的两个函数就是json_encode()和json_decode()。 这两个函数使用的时候有很多的主要事项,在这里我来说一下json_decode()。 json_decode(): 对JSON 格式的...
json_encode() 和 json_decode() 的面向对象的包装器,具有错误处理功能。 由在欧洲维也纳开发。 安装 安装 BraincraftedJson 的推荐方法是通过 : $ composer require braincrafted/json:@stable 提示:您应该...
虽然JS解码JSON非常方便,但是编码似乎没有什么好办法… 本着能懒即懒,不能懒也尽量懒的原则,古狗了一下 还真让我发现了 PHP里直接用json_decode就可以解码,用起来相当方便 代码如下: function json_encode_js...
从5.2版本开始,PHP原生提供json_encode()和json_decode()函数,前者用于编码,后者用于解码。下面我们来分析下这2个函数