温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何在PHP中让json_encode不转义中文

发布时间:2020-07-22 11:21:07 来源:亿速云 阅读:344 作者:Leah 栏目:编程语言

今天就跟大家聊聊有关如何在PHP中让json_encode不转义中文,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

PHP如何让json_encode不转义中文?

PHP让json_encode不转义中文的方法:在使用“json_encode()”函数进行JSON编码时,在第2个参数传入常量“JSON_UNESCAPED_UNICODE”,其意义是以字面编码多字节Unicode字符。

代码示例

$jsonStr = json_encode($data,JSON_UNESCAPED_UNICODE);

JSON 预定义常量

JSON_ERROR_NONE (integer)

没有错误发生。自 PHP 5.3.0 起生效。

JSON_ERROR_DEPTH (integer)

到达了最大堆栈深度。自 PHP 5.3.0 起生效。

JSON_ERROR_STATE_MISMATCH (integer)

出现了下溢(underflow)或者模式不匹配。自 PHP 5.3.0 起生效。

JSON_ERROR_CTRL_CHAR (integer)

控制字符错误,可能是编码不对。自 PHP 5.3.0 起生效。

JSON_ERROR_SYNTAX (integer)

语法错误。 自 PHP 5.3.0 起生效。

JSON_ERROR_UTF8 (integer)

异常的 UTF-8 字符,也许是因为不正确的编码。 自 PHP 5.3.3 起生效。

JSON_ERROR_RECURSION (integer)

The object or array passed to json_encode() include recursive references and cannot be encoded. If the JSON_PARTIAL_OUTPUT_ON_ERROR option was given, NULL will be encoded in the place of the recursive reference. Available since PHP 5.5.0.

JSON_ERROR_INF_OR_NAN (integer)

The value passed to json_encode() includes either NAN or INF. If the JSON_PARTIAL_OUTPUT_ON_ERROR option was given, 0 will be encoded in the place of these special numbers. Available since PHP 5.5.0.

JSON_ERROR_UNSUPPORTED_TYPE (integer)

A value of an unsupported type was given to json_encode(), such as a resource. If the JSON_PARTIAL_OUTPUT_ON_ERROR option was given, NULL will be encoded in the place of the unsupported value. Available since PHP 5.5.0.

JSON_ERROR_INVALID_PROPERTY_NAME (integer)

A key starting with \u0000 character was in the string passed to json_decode() when decoding a JSON object into a PHP object. Available since PHP 7.0.0.

JSON_ERROR_UTF16 (integer)

Single unpaired UTF-16 surrogate in unicode escape contained in the JSON string passed to json_encode(). Available since PHP 7.0.0.

下面的常量可以和 json_decode() 的 form 选项结合使用。

JSON_BIGINT_AS_STRING (integer)

将大数字编码成原始字符原来的值。 自 PHP 5.4.0 起生效。

JSON_OBJECT_AS_ARRAY (integer)

Decodes JSON objects as PHP array. This option can be added automatically by calling json_decode() with the second parameter equal to TRUE. Available since PHP 5.4.0.

下面的常量可以和 json_encode() 的 form 选项结合使用。

JSON_HEX_TAG (integer)

所有的 < 和 > 转换成 \u003C 和 \u003E。 自 PHP 5.3.0 起生效。

JSON_HEX_AMP (integer)

所有的 & 转换成 \u0026。 自 PHP 5.3.0 起生效。

JSON_HEX_APOS (integer)

所有的 ' 转换成 \u0027。 自 PHP 5.3.0 起生效。

JSON_HEX_QUOT (integer)

所有的 " 转换成 \u0022。 自 PHP 5.3.0 起生效。

JSON_FORCE_OBJECT (integer)

使一个非关联数组输出一个类(Object)而非数组。 在数组为空而接受者需要一个类(Object)的时候尤其有用。 自 PHP 5.3.0 起生效。

JSON_NUMERIC_CHECK (integer)

将所有数字字符串编码成数字(numbers)。 自 PHP 5.3.3 起生效。

JSON_PRETTY_PRINT (integer)

用空白字符格式化返回的数据。 自 PHP 5.4.0 起生效。

JSON_UNESCAPED_SLASHES (integer)

不要编码 /。 自 PHP 5.4.0 起生效。

JSON_UNESCAPED_UNICODE (integer)

以字面编码多字节 Unicode 字符(默认是编码成 \uXXXX)。 自 PHP 5.4.0 起生效。

JSON_PARTIAL_OUTPUT_ON_ERROR (integer)

Substitute some unencodable values instead of failing. Available since PHP 5.5.0.

JSON_PRESERVE_ZERO_FRACTION (integer)

Ensures that float values are always encoded as a float value. Available since PHP 5.6.6.

JSON_UNESCAPED_LINE_TERMINATORS (integer)

The line terminators are kept unescaped when JSON_UNESCAPED_UNICODE is supplied. It uses the same behaviour as it was before PHP 7.1 without this constant. Available since PHP 7.1.0.

看完上述内容,你们对如何在PHP中让json_encode不转义中文有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI