(PHP 5 >= 5.2.0, PHP 7, PHP 8, PECL json >= 1.2.0)
json_decode — 对 JSON 格式的字符串进行解码
json_decode(
string $json,
bool $assoc = false,
int $depth = 512,
int $options = 0
): mixed
接受一个 JSON 编码的字符串并且把它转换为 PHP 变量
json
待解码的 json string 格式的字符串。
这个函数仅能处理 UTF-8 编码的数据。
assoc
当该参数为 true 时,将返回 array 而非 object 。
depth
指定递归深度。
options
由 JSON_BIGINT_AS_STRING, JSON_INVALID_UTF8_IGNORE, JSON_INVALID_UTF8_SUBSTITUTE, JSON_OBJECT_AS_ARRAY, JSON_THROW_ON_ERROR 组成的掩码。 这些常量的行为在JSON constants页面有进一步描述。
通过恰当的 PHP 类型返回在 json 中编码的数据。值true, false 和 null 会相应地返回 true, false 和 null。 如果 json 无法被解码, 或者编码数据深度超过了递归限制的话,将会返回null 。
| 版本 | 说明 |
|---|---|
| 7.3.0 | JSON_THROW_ON_ERROR options was added. |
| 7.2.0 | JSON_INVALID_UTF8_IGNORE, and JSON_INVALID_UTF8_SUBSTITUTE options were added. |
| 7.1.0 | An empty JSON key ("") can be encoded to the empty object property instead of using a key with value _empty_. |
| 7.0.0 | Rejected RFC 7159 incompatible number formats - top level (07, 0xff, .1, -.1) and all levels ([1.], [1.e1]) |
| 7.0.0 | An empty PHP string or value that after casting to string is an empty string (NULL, FALSE) results in JSON syntax error. |
| 5.6.0 | Invalid non-lowercased variants of the true, false and null literals are no longer accepted as valid input, and will generate warnings. |
| 5.4.0 | JSON_BIGINT_AS_STRING, and JSON_OBJECT_AS_ARRAY options were added. |
| 5.4.0 | The options parameter was added. |
| 5.3.0 | Added the optional depth. The default recursion depth was increased from 128 to 512 |
| 5.2.3 | The nesting limit was increased from 20 to 128 |
| 5.2.1 | Added support for JSON decoding of basic types. |
参考地址:https://www.php.net/manual/zh/function.json-decode.php