搜索
首页后端开发PHP问题PHP如何让json_encode不转义中文?

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)

所有的 92d33664ec5f827b86e068a341370a86 转换成 \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

以上是PHP如何让json_encode不转义中文?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
酸与基本数据库:差异和何时使用。酸与基本数据库:差异和何时使用。Mar 26, 2025 pm 04:19 PM

本文比较了酸和基本数据库模型,详细介绍了它们的特征和适当的用例。酸优先确定数据完整性和一致性,适合财务和电子商务应用程序,而基础则侧重于可用性和

PHP安全文件上传:防止与文件相关的漏洞。PHP安全文件上传:防止与文件相关的漏洞。Mar 26, 2025 pm 04:18 PM

本文讨论了确保PHP文件上传的确保,以防止诸如代码注入之类的漏洞。它专注于文件类型验证,安全存储和错误处理以增强应用程序安全性。

PHP输入验证:最佳实践。PHP输入验证:最佳实践。Mar 26, 2025 pm 04:17 PM

文章讨论了PHP输入验证以增强安全性的最佳实践,重点是使用内置功能,白名单方法和服务器端验证等技术。

PHP API率限制:实施策略。PHP API率限制:实施策略。Mar 26, 2025 pm 04:16 PM

本文讨论了在PHP中实施API速率限制的策略,包括诸如令牌桶和漏水桶等算法,以及使用Symfony/Rate-limimiter之类的库。它还涵盖监视,动态调整速率限制和手

php密码哈希:password_hash和password_verify。php密码哈希:password_hash和password_verify。Mar 26, 2025 pm 04:15 PM

本文讨论了使用password_hash和pyspasswify在PHP中使用密码的好处。主要论点是,这些功能通过自动盐,强大的哈希算法和SECH来增强密码保护

OWASP前10 php:描述并减轻常见漏洞。OWASP前10 php:描述并减轻常见漏洞。Mar 26, 2025 pm 04:13 PM

本文讨论了OWASP在PHP和缓解策略中的十大漏洞。关键问题包括注射,验证损坏和XSS,并提供用于监视和保护PHP应用程序的推荐工具。

PHP XSS预防:如何预防XSS。PHP XSS预防:如何预防XSS。Mar 26, 2025 pm 04:12 PM

本文讨论了防止PHP中XSS攻击的策略,专注于输入消毒,输出编码以及使用安全增强的库和框架。

PHP接口与抽象类:何时使用。PHP接口与抽象类:何时使用。Mar 26, 2025 pm 04:11 PM

本文讨论了PHP中接口和抽象类的使用,重点是何时使用。界面定义了无实施的合同,适用于无关类和多重继承。摘要类提供常见功能

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!