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

来源清源教育

Hostiger 16G大内存特价VPS:伊斯坦布尔机房,1核50G SSD硬盘200Mbps带宽不限流量$59/年

国外主机测评昨天接到Hostigger(现Hostiger)商家邮件推送,称其又推出了一款特价大内存VPS,机房位于土耳其的亚欧交界城市伊斯坦布尔,核50G SSD硬盘200Mbps带宽不限月流量只要$59/年。 最近一次分享的促销信息还是5月底,当时商家推出的是同机房同配置的大内存VPS,价格是$59.99/年,不过内存只有10G,虽然同样是大内存,但想必这次商家给出16G,价格却是$59/年,...

星梦云60元夏日促销,四川100G高防4H4G10M,西南高防月付特价

星梦云怎么样?星梦云好不好,资质齐全,IDC/ISP均有,从星梦云这边租的服务器均可以备案,属于一手资源,高防机柜、大带宽、高防IP业务,一手整C IP段,四川电信,星梦云专注四川高防服务器,成都服务器,雅安服务器 。官方网站:点击访问星梦云官网活动方案:1、成都电信年中活动机(封锁UDP,不可解封):机房CPU内存硬盘带宽IP防护流量原价活动价开通方式成都电信优化线路4vCPU4G40G+50...

A400互联1H/1G/10M/300G流量37.8元/季

A400互联是一家成立于2020年的商家,本次给大家带来的是,全新上线的香港节点,cmi+cn2线路,全场香港产品7折优惠,优惠码0711,A400互联,只为给你提供更快,更稳,更实惠的套餐。目前,商家推出香港cn2节点+cmi线路云主机,1H/1G/10M/300G流量,37.8元/季,云上日子,你我共享。A400互联优惠码:七折优惠码:0711A400互联优惠方案:适合建站,个人开发爱好者配置...

json_encode为你推荐
浏览器哪个好大家用过的哪种浏览器最好用?用过多种浏览器的说电脑管家和360哪个好360和电脑管家哪个好啊音乐播放器哪个好目前音质最好的音乐播放器手机杀毒哪个好手机杀毒软件哪个最好用网络机顶盒哪个好什么牌子的网络机顶盒最好辽宁联通网上营业厅辽宁联通网上营业厅为什么不能交小灵通的话费?360云盘论坛360云盘,百度云盘,115云盘,哪个好用一点,理由是什么360云盘论坛360云盘最大多少G360云盘论坛怎么在百度贴吧中分享360云盘的资源?具体些360云安全中心我的电脑上的360杀毒软件老显示未连接至360云安全中心。
网站域名备案 budgetvm 骨干网 5折 加勒比群岛 sugarsync win8升级win10正式版 NetSpeeder 个人空间申请 100x100头像 空间出租 网通服务器托管 1美金 东莞服务器 vul 独立主机 工信部网站备案查询 photobucket lamp的音标 域名和主机 更多