Home  >  Article  >  php教程  >  【代码】PHP的json

【代码】PHP的json

WBOY
WBOYOriginal
2016-06-06 19:58:491081browse

json 的优点就不说了, 有个习惯,我在输出json的时候,喜欢用 sprintf 拼成json式, 前两天被朋友说不标准,必须要用json_encode生成的才是标准的json式,我当然很郁闷啦, 用了这么多年了,刚知道 这样做不标准,既然说我不标准,那上面才是标准的json式?

json的优点就不说了,


有个习惯,我在输出json的时候,喜欢用 sprintf 拼成json格式,


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


用了这么多年了,刚知道 这样做不标准,既然说我不标准,那上面才是标准的json格式?


{a : 'abc'}
{'a' : 'abc'}
{a : "abc"}
{"a" : "abc"}


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


我这么做


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


必然也符合标准。

既然如此,那我就要刨根问底,json_encode生成的json格式究竟有什么不同?
上代码

static 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_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); //输出NULL
                        break;
 
                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应该是5.3版本之后才支持的,由以下常量组成的二进制掩码: 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.虽然我没用过。。。
static void json_escape_string(smart_str *buf, char *s, int len, int options TSRMLS_DC) /* {{{ */
{
        int pos = 0;
        unsigned short us;
        unsigned short *utf16;
 
        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;
                }
 
        }
 
        utf16 = (unsigned short *) safe_emalloc(len, sizeof(unsigned short), 0);
        len = utf8_to_utf16(utf16, s, len); //这里会对你输入的值一次处理转成对应的Dec码,比如1是49,a是97这样的,保存到utf16中。
        if (len <br>
<br>
<span> </span><br>
<span>//下面这一段代码就是将一些特殊字符转义如 双引号,反斜线等等</span><br>
<pre class="brush:php;toolbar:false">while (pos ':
                                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;
                                        smart_str_appendc(buf, digits[us & ((1 >= 4;
                                        smart_str_appendc(buf, digits[us & ((1 >= 4;
                                        smart_str_appendc(buf, digits[us & ((1 <br>
<br>
<span>再来看看数组和对象,也很简单,</span><br>
<br>
<pre class="brush:php;toolbar:false">


通过简单分析,证明了一个问题,跟我上面用sprintf的方法其实是一样的,都是拼接字符串,


而且 为了性能,更应该鼓励用sprintf来拼接json格式,


因为 json_encode会进行很多 循环操作,而且所消耗的性能是线性的。


当然,此文只是在客观情况下分析了最简单的情况,如果数据是数组或者其他复杂的数据,当然还是建议用json_encode来做。


以上是本文关于PHP的json_encode分析,希望本文对广大php开发者有所帮助,感谢阅读本文。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:传智播客PHP1220班SHOPNext article:Win8的IIS中架设php