json_decode

审核中 原生PHP 未结 已结 置顶 精帖
删除 置顶 取消置顶 加精 取消加精
66 0
yswl
yswl VIP3 2021-09-04 10:18:59
悬赏:60金币 编辑此贴

(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 编码的数据。

    注意:

    PHP 实现了 JSON 的一个超集,参考 » RFC 7159.

  • assoc

  • 当该参数为 true 时,将返回 array 而非 object 。

  • depth

  • 指定递归深度。

  • options

  • 由 JSON_BIGINT_AS_STRINGJSON_INVALID_UTF8_IGNOREJSON_INVALID_UTF8_SUBSTITUTEJSON_OBJECT_AS_ARRAYJSON_THROW_ON_ERROR 组成的掩码。 这些常量的行为在JSON constants页面有进一步描述。

返回值

通过恰当的 PHP 类型返回在 json 中编码的数据。值truefalse 和 null 会相应地返回 truefalse 和 null。 如果 json 无法被解码, 或者编码数据深度超过了递归限制的话,将会返回null 。

更新日志


版本说明
7.3.0JSON_THROW_ON_ERROR options was added.
7.2.0JSON_INVALID_UTF8_IGNORE, and JSON_INVALID_UTF8_SUBSTITUTE options were added.
7.1.0An empty JSON key ("") can be encoded to the empty object property instead of using a key with value _empty_.
7.0.0Rejected RFC 7159 incompatible number formats - top level (07, 0xff, .1, -.1) and all levels ([1.], [1.e1])
7.0.0An empty PHP string or value that after casting to string is an empty string (NULLFALSE) results in JSON syntax error.
5.6.0Invalid non-lowercased variants of the truefalse and null literals are no longer accepted as valid input, and will generate warnings.
5.4.0JSON_BIGINT_AS_STRING, and JSON_OBJECT_AS_ARRAY options were added.
5.4.0The options parameter was added.
5.3.0Added the optional depth. The default recursion depth was increased from 128 to 512
5.2.3The nesting limit was increased from 20 to 128
5.2.1Added support for JSON decoding of basic types.

参考地址:https://www.php.net/manual/zh/function.json-decode.php