search
Homephp教程php手册PHP的json_encode分析

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

json的优点就不说了,

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

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

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

1

2

3

4

{a : 'abc'}

{'a' : 'abc'}

{a : "abc"}

{"a" : "abc"}

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

我这么做

1

2

$ret_json='{"%s":"%s"}';

echo json_encode($ret_json,"a","abc");

必然也符合标准。

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

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", &parameter, &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是主要的操作

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

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。
最复杂的是 字符串 、数组 、对象这三种类型,数组和对象是同一种操作。
先看看字符串吧,很长,注释直接写在代码里。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

//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 <code>//如果len小于0 说明出错。如果用json_encode处理GBK的编码,就会在这里挂掉。

                if (utf16) {

                        efree(utf16);

                }

                if (len

                        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, '"'); //输入 \"

 

//下面这一段代码就是将一些特殊字符转义如 双引号,反斜线等等

        while (pos

        {

                us = utf16[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 '<code>:

                                if (options & PHP_JSON_HEX_TAG) {

                                        smart_str_appendl(buf, "\\u003C", 6);

                                } else {

                                        smart_str_appendc(buf, '<code>);

                                }

                                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

                                        us >>= 4;

                                        smart_str_appendc(buf, digits[us & ((1

                                        us >>= 4;

                                        smart_str_appendc(buf, digits[us & ((1

                                        us >>= 4;

                                        smart_str_appendc(buf, digits[us & ((1

                                }

                                break;

                }

        }

        smart_str_appendc(buf, '"'); //结束 双引号。

        efree(utf16);

}

再来看看数组和对象,也很简单,

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

static 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, '"');

                                 &n

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
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么查找字符串是第几位php怎么查找字符串是第几位Apr 22, 2022 pm 06:48 PM

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft