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

来源清源教育

TTcloud:日本独立服务器促销活动,价格$70/月起,季付送10Mbps带宽

ttcloud怎么样?ttcloud是一家海外服务器厂商,运营服务器已经有10年时间,公司注册地址在香港地区,业务范围包括服务器托管,机柜托管,独立服务器等在内的多种服务。我们后台工单支持英文和中文服务。TTcloud最近推出了新上架的日本独立服务器促销活动,价格 $70/月起,季付送10Mbps带宽。也可以跟进客户的需求进行各种DIY定制。点击进入:ttcloud官方网站地址TTcloud拥有自...

spinservers($89/月),圣何塞10Gbps带宽服务器,达拉斯10Gbps服务器

spinservers是Majestic Hosting Solutions LLC旗下站点,主要提供国外服务器租用和Hybrid Dedicated等产品的商家,数据中心包括美国达拉斯和圣何塞机房,机器一般10Gbps端口带宽,高配置硬件,支持使用PayPal、信用卡、支付宝或者微信等付款方式。目前,商家针对部分服务器提供优惠码,优惠后达拉斯机房服务器最低每月89美元起,圣何塞机房服务器最低每月...

腾讯云轻量服务器老用户续费优惠和老用户复购活动

继阿里云服务商推出轻量服务器后,腾讯云这两年对于轻量服务器的推广力度还是比较大的。实际上对于我们大部分网友用户来说,轻量服务器对于我们网站和一般的业务来说是绝对够用的。反而有些时候轻量服务器的带宽比CVM云服务器够大,配置也够好,更有是价格也便宜,所以对于初期的网站业务来说轻量服务器是够用的。这几天UCLOUD优刻得香港服务器稳定性不佳,于是有网友也在考虑搬迁到腾讯云服务器商家,对于轻量服务器官方...

json_encode为你推荐
免费个人网站制作怎样制作个人网站啊?要免费的!软银支付软银支付是帮忙还信用卡的吗,可靠吗浏览器哪个好什么浏览器最好?涡轮增压和自然吸气哪个好涡轮增压和自然吸气哪个好电脑杀毒软件哪个好电脑杀毒用哪个好?英语词典哪个好买什么英语词典比较好行车记录仪哪个好请问行车记录仪那个牌子好?考生个人空间登录我是2007年入的学2010年毕业我想查询这3年的成绩,怎么办啊?求解答!360云盘共享群360云盘怎么找共享群强生美瞳月抛强生隐形眼镜月抛有哪些,价格是多少?哪个较好?
电信服务器租用 美国和欧洲vps securitycenter plesk ion sugarsync 免费名片模板 河南m值兑换 双十一秒杀 佛山高防服务器 qq对话框 华为云盘 华为云服务登录 根服务器 登陆空间 美国凤凰城 lamp是什么意思 免费蓝钻 免费php空间 免费稳定空间 更多