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

来源清源教育

BuyVM迈阿密KVM上线,AMD Ryzen 3900X+NVMe硬盘$2/月起

BuyVM在昨天宣布上线了第四个数据中心产品:迈阿密,基于KVM架构的VPS主机,采用AMD Ryzen 3900X CPU,DDR4内存,NVMe硬盘,1Gbps带宽,不限制流量方式,最低$2/月起,支持Linux或者Windows操作系统。这是一家成立于2010年的国外主机商,提供基于KVM架构的VPS产品,数据中心除了新上的迈阿密外还包括美国拉斯维加斯、新泽西和卢森堡等,主机均为1Gbps带...

gcorelabs:CDN业务节点分布100多个国家地区,免费版提供1T/月流量

卢森堡商家gcorelabs是个全球数据中心集大成的运营者,不但提供超过32个数据中心的VPS、13个数据中心的cloud(云服务器)、超过44个数据中心的独立服务器,还提供超过100个数据中心节点的CDN业务。CDN的总带宽容量超过50Tbps,支持免费测试! Gcorelabs根据业务分,有2套后台,分别是: CDN、流媒体平台、DDoS高防业务、块存储、cloud云服务器、裸金属服务器...

hostkvm:7折优惠-香港VPS韩国VPS,8折优惠-日本软银、美国CN2 GIA、新加坡直连VPS

hostkvm本月对香港国际线路的VPS、韩国CN2+bgp线路的VPS正在做7折终身优惠,对日本软银线路、美国CN2 GIA线路、新加坡直连线路的VPS进行8折终身优惠促销。所有VPS从4G内存开始支持Windows系统,当然主流Linux发行版是绝对不会缺席的!官方网站:https://hostkvm.com香港国际线路、韩国,7折优惠码:2021summer日本、美国、新加坡,8折优惠码:2...

json_encode为你推荐
绿色桌面背景图片win7如何把的“计算机”背景设置成绿色苹果x和xr哪个好苹果x和xr哪个好?有何区别?电脑杀毒软件哪个好电脑杀毒用哪个好?免费阅读小说app哪个好有什么好用的看小说的app华为p40和mate30哪个好mate30 5G和p40 5G有什么区别?手机杀毒软件哪个好手机安全杀毒软件哪个比较好些?云盘哪个好免费的网盘哪个好用啊?如何增加百度收录如何提高百度收录率?网通dns服务器地址新乡网通DNS服务器地址是什么上海dns服务器地址中国主要城市DNS服务器IP地址列表
域名备案流程 怎样申请域名 godaddy优惠码 vmsnap3 缓存服务器 外国域名 轻量 ca4249 空间论坛 softbank邮箱 老左正传 新家坡 华为云服务登录 免费的asp空间 英国伦敦 中国linux 徐州电信 购买空间 google搜索打不开 上海联通 更多