格式PHP的json_encode详解分析

json_encode  时间:2021-01-26  阅读:()

PHP的json_encode解分析json的点就不了

有个 我在出json的候喜用sprintf拼成json格式

前两天被朋友不准 必要用json_encode生成的才是准的json格式我当然很郁啦

用了 多年了 知道 做不准既然我不准 那上面才是准的json格式

[php] view plaincopy

{a : 'abc'}

{'a' : 'abc'}

{a : "abc"}

{"a" : "abc"}

那都知道 只有第四才是准的json格式。

我 做

[php] view plaincopy

$ret_json='{"%s":"%s"}' ;echo json_encode($ret_json,"a","abc");

必然也符合准。 PS  ^_^ 的php 交流群 276167802 wk

既然如此 那我就要刨根底 json_encode生成的json格式究竟有什不同

上代

[php] view plaincopystatic PHP_FUNCTION(json_encode)

{zval *parameter;smart_str buf = {0};long options = 0;if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|l", ¶meter,&options) == FAILURE){return;

}

JSON_G(error_code) = PHP_JSON_ERROR_NONE;php_json_encode(&buf, parameter, options TSRMLS_CC);

ZVAL_STRINGL(return_value, buf c, buf len, 1);smart_str_free(&buf);

}

JSON_G(error_code) = PHP_JSON_ERROR_NONE;

是定的json  可以通json_last_error函数取你用 反正我没用 。

php_json_encode是主要的操作

[php] view plaincopy

PHP_JSON_API void php_json_encode(smart_str *buf, zval *val, int options TSRMLS_DC) /* {{{ */{switch (Z_TYPE_P(val))

{case IS_NULL:smart_str_appendl(buf, "null", 4); //出NULLbreak;case IS_BOOL:if (Z_BVAL_P(val)) {smart_str_appendl(buf, "true", 4);//出true

} else {smart_str_appendl(buf, "false", 5);//出false

}break;case IS_LONG:smart_str_append_long(buf,Z_LVAL_P(val));//出整形的break;case IS_DOUBLE:

{char *d =NULL;int len;double dbl =Z_DVAL_P(val);if (!zend_isinf(dbl)&&!zend_isnan(dbl)) {//非无尽len = spprintf(&d, 0, "%*k", (int) EG(precision), dbl);smart_str_appendl(buf, d, len);efree(d);

} else {php_error_docref(NULL TSRMLS_CC,E_WARNING, "double%9g does not conform to the JSON spec, encoded as 0", dbl);smart_str_appendc(buf, '0');

}

}break;case IS_STRING://字符串json_escape_string(buf,Z_STRVAL_P(val),Z_STRLEN_P(val), options TSRMLS_CC);break;

case IS_ARRAY://数和象case IS_OBJECT:json_encode_array(buf,&val, options TSRMLS_CC);break;default:php_error_docref(NULL TSRMLS_CC,E_WARNING, "type is unsupported, encoded as null");smart_str_appendl(buf, "null", 4);break;

}return;

}

很明根据不同的型会有相的case 。

最 的是字符串 、数 、象三 型数和象是同一操作。

先看看字符串吧很 注直接写在代里。

//options 是53版本之后才支持的 由以下常量成的二制掩  JSON_HEX_QUOT, JSON_HEX_TAG,JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT, JSON_UNESCAPED_UNICODE 然我没用 。 。 。[php] view plaincopystatic void json_escape_string(smart_str *buf, char *s, int len, int options TSRMLS_DC) /* {{{ */{int pos = 0;unsigned short us;unsigned short *utf 16;if (len == 0) {//如果度0 直接返回双引号""smart_str_appendl(buf, "\"\"", 2);return;

}if (options&PHP_JSON_NUMERIC_CHECK) {// 是否 0-9的数字 如果是数字 那就会直接把数据作long或double型返回 。double d;int type;long p;if ((type = is_numeric_string(s, len,&p,&d, 0)) != 0) {if (type == IS_LONG) {smart_str_append_long(buf, p);

} else if (type == IS_DOUBLE) {if (!zend_isinf(d)&&!zend_isnan(d)) {char *tmp;

int l = spprintf(&tmp, 0, "%*k", (int) EG(precision), d);smart_str_appendl(buf, tmp, l);efree(tmp);

} else {php_error_docref(NULL TSRMLS_CC,E_WARNING, "double%9g does not conform to the JSON spec, encoded as 0", d);smart_str_appendc(buf, '0');

}

}return;

}

}utf 16 = (unsigned short *) safe_emalloc(len, sizeof(unsigned short), 0);len = utf8_to_utf 16(utf 16, s, len); //里会你入的一次理成 的 Dec  比如1是49 a是97 的保存到utf 16中 。if (len <= 0) {//如果len小于0 明出 。如果用json_encode理GBK的 就会在里挂掉。if (utf 16) {efree(utf 16);

}if (len < 0) {

JSON_G(error_code) = PHP_JSON_ERROR_UTF8;if (!PG(display_errors)) {php_error_docref(NULL TSRMLS_CC,E_WARNING, "Invalid UTF-8 sequence in argument");}smart_str_appendl(buf, "null", 4);

} else {smart_str_appendl(buf, "\"\"", 2);

}return;

}smart_str_appendc(buf, '"'); //入\"

//下面一段代就是将一些特殊字符 如双引号 反斜等等

[php] view plaincopywhile (pos < len)

{us = utf 16[pos++];switch (us)

{

case '"' :if (options&PHP_JSON_HEX_QUOT) {smart_str_appendl(buf, "\\u0022", 6);} else {smart_str_appendl(buf, "\\\"", 2);

}break;case '\\' :smart_str_appendl(buf, "\\\\", 2);break;case '/' :smart_str_appendl(buf, "\\/", 2);break;case '\b' :smart_str_appendl(buf, "\\b", 2);break;case '\f' :smart_str_appendl(buf, "\\f", 2);break;case '\n' :smart_str_appendl(buf, "\\n", 2);break;case '\r' :smart_str_appendl(buf, "\\r", 2);break;case '\t' :smart_str_appendl(buf, "\\t", 2);break;case '<' :if (options&PHP_JSON_HEX_TAG) {smart_str_appendl(buf, "\\u003C", 6);} else {smart_str_appendc(buf, '<');

}break;case '>' :if (options&PHP_JSON_HEX_TAG) {smart_str_appendl(buf, "\\u003E", 6);

} else {smart_str_appendc(buf, '>');

}break;case '&' :if (options&PHP_JSON_HEX_AMP) {smart_str_appendl(buf, "\\u0026", 6);

} else {smart_str_appendc(buf, '&');

}break;case '\' ' :if (options&PHP_JSON_HEX_APOS) {smart_str_appendl(buf, "\\u0027", 6);

} else {smart_str_appendc(buf, '\' ');

}break;default: //一直到里没有特殊字符就会把append到buf中if (us >= ' '&&(us&127) == us) {smart_str_appendc(buf, (unsigned char) us);

} else {smart_str_appendl(buf, "\\u", 2);us =REVERSE16(us);smart_str_appendc(buf, digits[us&((1 << 4) - 1)]);us >>= 4;smart_str_appendc(buf, digits[us&((1 << 4) - 1)]);us >>= 4;smart_str_appendc(buf, digits[us&((1 << 4) - 1)]);us >>= 4;smart_str_appendc(buf, digits[us&((1 << 4) - 1)]);}break;

}

}smart_str_appendc(buf, '"'); //束双引号。efree(utf 16);

}

再来看看数和象 也很 

[php] view plaincopystatic void json_encode_array(smart_str *buf, zval **val, int options TSRMLS_DC) /* {{{ */{int i, r;

HashTable *myht;if (Z_TYPE_PP(val) == IS_ARRAY) {myht =HASH_OF(*val);r = (options&PHP_JSON_FORCE_OBJECT) ? PHP_JSON_OUTPUT_OBJECT : json_determine_array_type(val TSRMLS_CC);

} else {myht =Z_OBJPROP_PP(val);r = PHP_JSON_OUTPUT_OBJECT;

}if (myht&&myht->nApplyCount > 1) {php_error_docref(NULL TSRMLS_CC,E_WARNING, "recursion detected");smart_str_appendl(buf, "null", 4);return;

}

//始if (r == PHP_JSON_OUTPUT_ARRAY) {smart_str_appendc(buf, ' [');

} else {smart_str_appendc(buf, '{');

}i =myht ? zend_hash_num_elements(myht) : 0;if (i > 0)

{char *key;zval **data;ulong index;uint key_len;

HashPosition pos;

HashTable *tmp_ht;int need_comma = 0;zend_hash_internal_pointer_reset_ex(myht,&pos);

//便利哈希表for (; ; zend_hash_move_forward_ex(myht,&pos)) {

i = zend_hash_get_current_key_ex(myht,&key,&key_len,&index, 0,&pos);if (i ==HASH_KEY_NON_EXISTANT)break;if (zend_hash_get_current_data_ex(myht, (void **)&data,&pos) == SUCCESS) {tmp_ht =HASH_OF(*data);if (tmp_ht) {tmp_ht->nApplyCount++;

}if (r == PHP_JSON_OUTPUT_ARRAY) {if (need_comma) {smart_str_appendc(buf, ' , ');

} else {need_comma = 1;

}

//将append到buf中php_json_encode(buf, *data, options TSRMLS_CC);

} else if (r == PHP_JSON_OUTPUT_OBJECT) {if (i ==HASH_KEY_IS_STRING) {if (key[0] == '\0'&&Z_TYPE_PP(val) == IS_OBJECT) {

/* Skip protected and private members */if (tmp_ht) {tmp_ht->nApplyCount--;

}continue;

}if (need_comma) {smart_str_appendc(buf, ' , ');

} else {need_comma = 1;

}json_escape_string(buf, key, key_len - 1, options&~PHP_JSON_NUMERIC_CHECK TSRMLS_CC);smart_str_appendc(buf, ' : ');php_json_encode(buf, *data, options TSRMLS_CC);

} else {if (need_comma) {smart_str_appendc(buf, ' , ');

} else {need_comma = 1;

}smart_str_appendc(buf, '"');smart_str_append_long(buf, (long) index);smart_str_appendc(buf, '"');smart_str_appendc(buf, ' : ');php_json_encode(buf, *data, options TSRMLS_CC);

}

}if (tmp_ht) {tmp_ht->nApplyCount--;

}

}

}

}

//束 PS  ^_^不的php 交流群 276167802  csl有趣的可以加入来一起 if (r == PHP_JSON_OUTPUT_ARRAY) {smart_str_appendc(buf, '] ');

} else {smart_str_appendc(buf, '}');

}

}

通 分析 明了一个 跟我上面用sprintf的方法其是一的都是拼接字符串

而且 了性能 更 鼓励用sprintf来拼接json格式

因 json_encode会行很多循操作 而且所消耗的性能是性的。

当然此文只是在客情况下分析了最 的情况 如果数据是数或者其他 的数据 当然是建用json_encode来做。

以上是本文于PHP的json_encode分析希望本文广大php 者有所帮助 感 本文。

来源清源教育

HostKvm($4.25/月)俄罗斯/香港高防VPS

HostKvm又上新了,这次上架了2个线路产品:俄罗斯和香港高防VPS,其中俄罗斯经测试电信CN2线路,而香港高防VPS提供30Gbps攻击防御。HostKvm是一家成立于2013年的国外主机服务商,主要提供基于KVM架构的VPS主机,可选数据中心包括日本、新加坡、韩国、美国、中国香港等多个地区机房,均为国内直连或优化线路,延迟较低,适合建站或者远程办公等。俄罗斯VPSCPU:1core内存:2G...

蓝速数据(58/年)秒杀服务器独立1核2G 1M

蓝速数据金秋上云季2G58/年怎么样?蓝速数据物理机拼团0元购劲爆?蓝速数据服务器秒杀爆产品好不好?蓝速数据是广州五联科技信息有限公司旗下品牌云计算平台、采用国内首选Zkeys公有云建设多种开通方式、具有IDC、ISP从业资格证IDC运营商新老用户值得信赖的商家。我司主要从事内地的枣庄、宿迁、深圳、绍兴、成都(市、县)。待开放地区:北京、广州、十堰、西安、镇江(市、县)。等地区数据中心业务,均KV...

inux国外美老牌PhotonVPS月$2.5 ,Linux系统首月半价

PhotonVPS 服务商我们是不是已经很久没有见过?曾经也是相当的火爆的,我们中文习惯称作为饭桶VPS主机商。翻看之前的文章,在2015年之前也有较多商家的活动分享的,这几年由于服务商太多,乃至于有一些老牌的服务商都逐渐淡忘。这不有看到PhotonVPS商家发布促销活动。PhotonVPS 商家七月份推出首月半价Linux系统VPS主机,首月低至2.5美元,有洛杉矶、达拉斯、阿什本机房,除提供普...

json_encode为你推荐
苹果手机助手哪个好苹果手机助手哪个好,苹果手机助手推荐?杰士邦和杜蕾斯哪个好杰士邦的超薄款跟杜蕾斯的超薄款,哪个舒服点?炒股软件哪个好用用手机股票软件哪个好雅思和托福哪个好考托福好考还是雅思好考哇?雅思和托福哪个好考考托福好还是雅思好云盘哪个好网络云盘哪个好用qq空间登录网站QQ空间打开需要输入用户名和密码,下面是正在连接一个网址和领域网址东莞电信宽带在东莞报装办理电信宽带好不好用,需要多少钱,在哪里报装网通dns服务器地址新乡网通DNS服务器地址是什么dns服务器什么意思DNS什么意思?
广西虚拟主机 域名估价 网站虚拟主机空间 传奇服务器租用 西安电信测速 liquidweb 256m内存 info域名 主机合租 华为网络硬盘 cpanel空间 天互数据 lol台服官网 nerds 服务器托管什么意思 免费测手机号 申请网站 网通服务器 东莞服务器托管 东莞主机托管 更多