格式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 者有所帮助 感 本文。

来源清源教育

RepriseHosting:$27.97/月-L5640,16G内存,1TB硬盘,10TB月流量,西雅图机房

RepriseHosting是成立于2012年的国外主机商,提供独立服务器租用和VPS主机等产品,数据中心在美国西雅图和拉斯维加斯机房。商家提供的独立服务器以较低的价格为主,目前针对西雅图机房部分独立服务器提供的优惠仍然有效,除了价格折扣外,还免费升级内存和带宽,商家支持使用支付宝或者PayPal、信用卡等付款方式。配置一 $27.97/月CPU:Intel Xeon L5640内存:16GB(原...

蓝竹云挂机宝25元/年,美国西雅图 1核1G 100M 20元

蓝竹云怎么样 蓝竹云好不好蓝竹云是新商家这次给我们带来的 挂机宝25元/年 美国西雅图云服务器 下面是套餐和评测,废话不说直接开干~~蓝竹云官网链接点击打开官网江西上饶挂机宝宿主机配置 2*E5 2696V2 384G 8*1500G SAS RAID10阵列支持Windows sever 2008,Windows sever 2012,Centos 7.6,Debian 10.3,Ubuntu1...

香港、美国、日本、韩国、新加坡、越南、泰国、加拿大、英国、德国、法国等VPS,全球独立服务器99元起步 湘南科技

全球独立服务器、站群多IP服务器、VPS(哪个国家都有),香港、美国、日本、韩国、新加坡、越南、泰国、加拿大、英国、德国、法国等等99元起步,湘南科技郴州市湘南科技有限公司官方网址:www.xiangnankeji.cn产品内容:全球独立服务器、站群多IP服务器、VPS(哪个国家都有),香港、美国、日本、韩国、新加坡、越南、泰国、加拿大、英国、德国、法国等等99元起步,湘南科技VPS价格表:独立服...

json_encode为你推荐
softbank手机日本softbank手机国内使用涡轮增压和自然吸气哪个好本田车自然吸气和涡轮增压哪个好自然吸气和涡轮增压哪个好汽车涡轮增压好还是自然吸气好游戏加速器哪个好网游加速器哪个最好用?浏览器哪个好用哪个浏览器比较好dns服务器地址如何知道自己的IP地址和DNS服务器地址?铁通dns服务器地址铁通dns服务器地址360云盘下载下载一个360云盘,怎么下载360云盘同步版360云盘和360云盘同步版有甚么区分同步版占用电脑空间?什么时候买车最便宜一年中什么时间段买车最便宜,最合适?
asp主机 安徽虚拟主机 域名主机基地 cn域名个人注册 ipage 电信测速器 GGC dreamhost 香港cdn 空间服务商 铁通流量查询 促正网秒杀 谁的qq空间最好看 股票老左 老左正传 个人免费邮箱 杭州电信 mteam 建站技术 windowsserver2008r2 更多