当前位置:首页 > PHP教程 > php高级应用 > 列表

PHP的json_encode使用分析说明

发布:smiling 来源: PHP粉丝网  添加日期:2014-08-27 15:35:35 浏览: 评论:0 

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格式究竟有什么不同?

实例代码如下:

  1. static PHP_FUNCTION(json_encode) 
  2.         zval *parameter; 
  3.         smart_str buf = {0}; 
  4.         long options = 0; 
  5.  
  6.         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|l", &parameter, &options) == FAILURE) { 
  7.                 return;//开源代码phpfensi.com 
  8.         }   
  9.  
  10.         JSON_G(error_code) = PHP_JSON_ERROR_NONE; 
  11.  
  12.         php_json_encode(&buf, parameter, options TSRMLS_CC); 
  13.  
  14.         ZVAL_STRINGL(return_value, buf.c, buf.len, 1); 
  15.  
  16.         smart_str_free(&buf); 

JSON_G(error_code) = PHP_JSON_ERROR_NONE;

是定义的json错误,该错误可以通过json_last_error函数获取,你用过吗?反正我没用过.

php_json_encode是主要的操作,代码如下:

  1. PHP_JSON_API void php_json_encode(smart_str *buf, zval *val, int options TSRMLS_DC) /* {{{ */ 
  2.         switch (Z_TYPE_P(val)) 
  3.         { 
  4.                 case IS_NULL
  5.                         smart_str_appendl(buf, "null", 4); //输出NULL 
  6.                         break
  7.  
  8.                 case IS_BOOL
  9.                         if (Z_BVAL_P(val)) { 
  10.                                 smart_str_appendl(buf, "true", 4);//输出true 
  11.                         } else { 
  12.                                 smart_str_appendl(buf, "false", 5);//输出false 
  13.                         } 
  14.                         break
  15.  
  16.                 case IS_LONG
  17.                         smart_str_append_long(buf, Z_LVAL_P(val));//输出长整形的值 
  18.                         break
  19.  
  20.                 case IS_DOUBLE
  21.                         { 
  22.                                 char *d = NULL; 
  23.                                 int len; 
  24.                                 double dbl = Z_DVAL_P(val); 
  25.  
  26.                                 if (!zend_isinf(dbl) && !zend_isnan(dbl)) {//非无穷尽 
  27.                                         len = spprintf(&d, 0, "%.*k", (int) EG(precision), dbl); 
  28.                                         smart_str_appendl(buf, d, len); 
  29.                                         efree(d); 
  30.                                 } else { 
  31.                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "double %.9g does not conform to the JSON spec, encoded as 0", dbl); 
  32.                                         smart_str_appendc(buf, '0'); 
  33.                                 } 
  34.                        } 
  35.                         break
  36.  
  37.                 case IS_STRING://字符串 
  38.                         json_escape_string(buf, Z_STRVAL_P(val), Z_STRLEN_P(val), options TSRMLS_CC); 
  39.                         break
  40.  
  41.                 case IS_ARRAY://数组和对象 
  42.                 case IS_OBJECT
  43.                         json_encode_array(buf, &val, options TSRMLS_CC); 
  44.                         break
  45.  
  46.                 default
  47.                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "type is unsupported, encoded as null"); 
  48.                         smart_str_appendl(buf, "null", 4); 
  49.                         break
  50.         } 
  51.  
  52.         return

很明显,根据不同的类型,会有相应的case,最复杂的是字符串 、数组 、对象这三种类型,数组和对象是同一种操作,先看看字符串吧,很长,注释直接写在代码里,代码如下:

  1. /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.虽然我没用过。。。 
  2. static void json_escape_string(smart_str *buf, char *s, int len, int options TSRMLS_DC) /* {{{ */ 
  3.         int pos = 0; 
  4.         unsigned short us; 
  5.         unsigned short *utf16; 
  6.  
  7.         if (len == 0) {//如果长度为0,则直接返回 双引号 "" 
  8.                 smart_str_appendl(buf, """", 2); 
  9.                 return
  10.         } 
  11.  
  12.         if (options & PHP_JSON_NUMERIC_CHECK) {//检测是否为0-9的数字,如果是数字,那么就会直接把数据作为long或double类型返回。 
  13.                 double d; 
  14.                 int type; 
  15.                 long p; 
  16.  
  17.                 if ((type = is_numeric_string(s, len, &p, &d, 0)) != 0) { 
  18.                         if (type == IS_LONG) { 
  19.                                 smart_str_append_long(buf, p); 
  20.                         } else if (type == IS_DOUBLE) { 
  21.                                 if (!zend_isinf(d) && !zend_isnan(d)) { 
  22.                                         char *tmp; 
  23.                                         int l = spprintf(&tmp, 0, "%.*k", (int) EG(precision), d); 
  24.                                         smart_str_appendl(buf, tmp, l); 
  25.                                         efree(tmp); 
  26.                                 } else { 
  27.                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "double %.9g does not conform to the JSON spec, encoded as 0", d); 
  28.                                         smart_str_appendc(buf, '0'); 
  29.                                 } 
  30.                         } 
  31.                         return
  32.                 } 
  33.  
  34.         } 
  35.  
  36.         utf16 = (unsigned short *) safe_emalloc(len, sizeof(unsigned short), 0); 
  37.         len = utf8_to_utf16(utf16, s, len); //这里会对你输入的值一次处理转成对应的Dec码,比如1是49,a是97这样的,保存到utf16中。 
  38.         if (len <= 0) {//如果len小于0 说明出错。如果用json_encode处理GBK的编码,就会在这里挂掉。 
  39.                 if (utf16) { 
  40.                         efree(utf16); 
  41.                 } 
  42.                 if (len < 0) { 
  43.                         JSON_G(error_code) = PHP_JSON_ERROR_UTF8; 
  44.                         if (!PG(display_errors)) { 
  45.                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid UTF-8 sequence in argument"); 
  46.                         } 
  47.                         smart_str_appendl(buf, "null", 4); 
  48.                 } else { 
  49.                         smart_str_appendl(buf, """", 2); 
  50.                 } 
  51.                 return
  52.         } 
  53.  
  54.         smart_str_appendc(buf, '"'); //输入 " 
  55.  
  56. //下面这一段代码就是将一些特殊字符转义如 双引号,反斜线等等 
  57.         while (pos < len) 
  58.         { 
  59.                 us = utf16[pos++]; 
  60.  
  61.                 switch (us) 
  62.                 { 
  63.                         case '"'
  64.                                 if (options & PHP_JSON_HEX_QUOT) { 
  65.                                         smart_str_appendl(buf, "\u0022", 6); 
  66.                                 } else { 
  67.                                         smart_str_appendl(buf, "\"", 2); 
  68.                                 } 
  69.                                 break
  70.  
  71.                         case '\'
  72.                                 smart_str_appendl(buf, "\\", 2); 
  73.                                 break
  74. case '/'
  75.                                 smart_str_appendl(buf, "\/", 2); 
  76.                                 break
  77.  
  78.                         case 'b'
  79.                                 smart_str_appendl(buf, "\b", 2); 
  80.                                 break
  81.  
  82.                         case 'f'
  83.                                 smart_str_appendl(buf, "\f", 2); 
  84.                                 break
  85.  
  86.                         case 'n'
  87.                                 smart_str_appendl(buf, "\n", 2); 
  88.                                 break
  89.  
  90.                         case 'r'
  91.                                 smart_str_appendl(buf, "\r", 2); 
  92.                                 break
  93.  
  94.                         case 't'
  95.                                 smart_str_appendl(buf, "\t", 2); 
  96.                                 break
  97.  
  98.                         case '<'
  99.                                 if (options & PHP_JSON_HEX_TAG) { 
  100.                                         smart_str_appendl(buf, "\u003C", 6); 
  101.                                 } else { 
  102.                                         smart_str_appendc(buf, '<'); 
  103.                                 } 
  104.                                 break
  105.  
  106.                         case '>'
  107.                                 if (options & PHP_JSON_HEX_TAG) { 
  108.                                         smart_str_appendl(buf, "\u003E", 6); 
  109.                                 } else { 
  110.                                         smart_str_appendc(buf, '>'); 
  111.                                 break
  112.  
  113.                         case '&'
  114.                                 if (options & PHP_JSON_HEX_AMP) { 
  115.                                         smart_str_appendl(buf, "\u0026", 6); 
  116.                                 } else { 
  117.                                         smart_str_appendc(buf, '&'); 
  118.                                 } 
  119.                                 break
  120.  
  121.                         case ''': 
  122.                                 if (options & PHP_JSON_HEX_APOS) { 
  123.                                         smart_str_appendl(buf, "\u0027", 6); 
  124.                                 } else { 
  125.                                         smart_str_appendc(buf, '''); 
  126.                                 } 
  127.                                 break
  128.  
  129.                         default//一直到这里,没有特殊字符就会把值append到buf中 
  130.                                 if (us >= ' ' && (us & 127) == us) { 
  131.                                         smart_str_appendc(buf, (unsigned char) us); 
  132.                                 } else { 
  133.                                         smart_str_appendl(buf, "\u", 2); 
  134.                                         us = REVERSE16(us); 
  135.  
  136.                                         smart_str_appendc(buf, digits[us & ((1 << 4) - 1)]); 
  137.                                         us >>= 4; 
  138.                                         smart_str_appendc(buf, digits[us & ((1 << 4) - 1)]); 
  139.                                         us >>= 4; 
  140.                                         smart_str_appendc(buf, digits[us & ((1 << 4) - 1)]); 
  141.                                         us >>= 4; 
  142.                                         smart_str_appendc(buf, digits[us & ((1 << 4) - 1)]); 
  143.                                 } 
  144.                                 break
  145.                 } 
  146.         } 
  147.         smart_str_appendc(buf, '"'); //结束 双引号。 
  148.         efree(utf16); 
  149. }  

再来看看数组和对象,也很简单,代码如下:

  1. static void json_encode_array(smart_str *buf, zval **val, int options TSRMLS_DC) /* {{{ */ 
  2.         int i, r; 
  3.         HashTable *myht; 
  4.  
  5.         if (Z_TYPE_PP(val) == IS_ARRAY) { 
  6.                 myht = HASH_OF(*val); 
  7.                 r = (options & PHP_JSON_FORCE_OBJECT) ? PHP_JSON_OUTPUT_OBJECT : json_determine_array_type(val TSRMLS_CC); 
  8.         } else { 
  9.                 myht = Z_OBJPROP_PP(val); 
  10.                 r = PHP_JSON_OUTPUT_OBJECT; 
  11.         }   
  12.  
  13.         if (myht && myht->nApplyCount > 1) { 
  14.                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "recursion detected"); 
  15.                 smart_str_appendl(buf, "null", 4); 
  16.                 return
  17.         } 
  18. //开始标签 
  19.         if (r == PHP_JSON_OUTPUT_ARRAY) { 
  20.                 smart_str_appendc(buf, '['); 
  21.         } else { 
  22.                 smart_str_appendc(buf, '{'); 
  23.         }   
  24.  
  25.         i = myht ? zend_hash_num_elements(myht) : 0; 
  26.  
  27.         if (i > 0) 
  28.         { 
  29.                 char *key; 
  30.                 zval **data; 
  31.                 ulong index; 
  32.                 uint key_len; 
  33.                 HashPosition pos; 
  34.                 HashTable *tmp_ht; 
  35.                 int need_comma = 0; 
  36.  
  37.                 zend_hash_internal_pointer_reset_ex(myht, &pos); 
  38. //便利哈希表 
  39.                 for (;; zend_hash_move_forward_ex(myht, &pos)) { 
  40.                         i = zend_hash_get_current_key_ex(myht, &key, &key_len, &index, 0, &pos); 
  41.                         if (i == HASH_KEY_NON_EXISTANT) 
  42.                                 break
  43.  
  44.                         if (zend_hash_get_current_data_ex(myht, (void **) &data, &pos) == SUCCESS) { 
  45.                                 tmp_ht = HASH_OF(*data); 
  46.                                 if (tmp_ht) { 
  47.                                         tmp_ht->nApplyCount++; 
  48.                                 } 
  49.  
  50.                                 if (r == PHP_JSON_OUTPUT_ARRAY) { 
  51.                                         if (need_comma) { 
  52.                                                 smart_str_appendc(buf, ','); 
  53.                                         } else { 
  54.                                                 need_comma = 1; 
  55.                                         } 
  56. //将值append到 buf中 
  57.                                         php_json_encode(buf, *data, options TSRMLS_CC); 
  58.                                 } else if (r == PHP_JSON_OUTPUT_OBJECT) { 
  59.                                         if (i == HASH_KEY_IS_STRING) { 
  60.                                                 if (key[0] == '' && Z_TYPE_PP(val) == IS_OBJECT) { 
  61.                                                         /* Skip protected and private members. */ 
  62.                                                         if (tmp_ht) { 
  63.                                                                 tmp_ht->nApplyCount--; 
  64.                                                         } 
  65.                                                         continue
  66.                                                 } 
  67.  
  68.                                                 if (need_comma) { 
  69.                                                         smart_str_appendc(buf, ','); 
  70.                                                 } else { 
  71.                                                         need_comma = 1; 
  72.                                                 } 
  73.  
  74.                                                 json_escape_string(buf, key, key_len - 1, options & ~PHP_JSON_NUMERIC_CHECK TSRMLS_CC); 
  75.                                                 smart_str_appendc(buf, ':'); 
  76.  
  77.                                                 php_json_encode(buf, *data, options TSRMLS_CC); 
  78.                                         } else { 
  79.                                                 if (need_comma) { 
  80.                                                         smart_str_appendc(buf, ','); 
  81.                                                 } else { 
  82.                                                         need_comma = 1; 
  83.                                                 } 
  84.  
  85.                                                 smart_str_appendc(buf, '"'); 
  86.                                                 smart_str_append_long(buf, (long) index); 
  87.                                                 smart_str_appendc(buf, '"'); 
  88.                                                 smart_str_appendc(buf, ':'); 
  89.  
  90.                                                 php_json_encode(buf, *data, options TSRMLS_CC); 
  91.                                         } 
  92.                                 } 
  93.  
  94.                                 if (tmp_ht) { 
  95.                                         tmp_ht->nApplyCount--; 
  96.                                 } 
  97.                         } 
  98.                 } 
  99.         } 
  100. //结束标签 
  101.         if (r == PHP_JSON_OUTPUT_ARRAY) { 
  102.                 smart_str_appendc(buf, ']'); 
  103.         } else { 
  104.                 smart_str_appendc(buf, '}'); 
  105.         } 
  106. }  

通过简单分析,证明了一个问题,跟我上面用sprintf的方法其实是一样的,都是拼接字符串,而且 为了性能,更应该鼓励用sprintf来拼接json格式,因为 json_encode会进行很多循环操作,而且所消耗的性能是线性的 O(n^2).

Tags: json_encode PHP使用分析

分享到: