json_decode

(PHP 5 >= 5.2.0, PECL json >= 1.2.0)

json_decode — 对 JSON 格式的字符串进行编码

说明 ¶

mixed json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )

接受一个 JSON 格式的字符串并且把它转换为 PHP 变量

参数 ¶

json

待解码的 json string 格式的字符串。

This function only works with UTF-8 encoded data.

assoc

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

depth

User specified recursion depth.(递归深度)

options

Bitmask of JSON decode options. Currently only JSON_BIGINT_AS_STRING is supported (default is to cast large integers as floats)(目前只支持JSON_BIGINT_AS_STRING,默认是将 大整数强制转换为浮点数。)

返回值 ¶

Returns the value encoded in json in appropriate PHP type. Values true, false and null (case-insensitive) are returned as TRUEFALSE and NULL respectively. NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.

范例 ¶

Example #1 json_decode() 的例子

<?php
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';

var_dump(json_decode($json));
var_dump(json_decode($json, true));

?>

以上例程会输出:

object(stdClass)#1 (5) {["a"] => int(1)["b"] => int(2)["c"] => int(3)["d"] => int(4)["e"] => int(5)
}array(5) {["a"] => int(1)["b"] => int(2)["c"] => int(3)["d"] => int(4)["e"] => int(5)
}

Example #2 Accessing invalid object properties

Accessing elements within an object that contain characters not permitted under PHP's naming convention (e.g. the hyphen) can be accomplished by encapsulating the element name within braces and the apostrophe.(当访问对象的元素时,如果元素名是字符串,需要用大扩号和但引号扩起来)

<?php

$json = '{"foo-bar": 12345}';

$obj = json_decode($json);
print $obj->{'foo-bar'}; // 12345

?>

Example #3 common mistakes using json_decode()

<?php

// the following strings are valid JavaScript but not valid JSON

// the name and value must be enclosed in double quotes
// single quotes are not valid 
$bad_json = "{ 'bar': 'baz' }";
json_decode($bad_json); // null

// the name must be enclosed in double quotes
$bad_json = '{ bar: "baz" }';
json_decode($bad_json); // null

// trailing commas are not allowed
$bad_json = '{ bar: "baz", }';
json_decode($bad_json); // null

?>

Example #4 depth errors

<?php
// Encode the data.
$json = json_encode(
    array(
        1 => array(
            'English' => array(
                'One',
                'January'
            ),
            'French' => array(
                'Une',
                'Janvier'
            )
        )
    )
);

// Define the errors.
$constants = get_defined_constants(true);
$json_errors = array();
foreach ($constants["json"] as $name => $value) {
    if (!strncmp($name, "JSON_ERROR_", 11)) {
        $json_errors[$value] = $name;
    }
}

// Show the errors for different depths.
foreach (range(4, 3, -1) as $depth) {
    var_dump(json_decode($json, true, $depth));
    echo 'Last error: ', $json_errors[json_last_error()], PHP_EOL, PHP_EOL;
}
?>

以上例程会输出:

array(1) {[1]=>array(2) {["English"]=>array(2) {[0]=>string(3) "One"[1]=>string(7) "January"}["French"]=>array(2) {[0]=>string(3) "Une"[1]=>string(7) "Janvier"}}
}
Last error: JSON_ERROR_NONENULL
Last error: JSON_ERROR_DEPTH

Example #5 json_decode() of large integers

<?php
$json = '12345678901234567890';

var_dump(json_decode($json));
var_dump(json_decode($json, false, 512, JSON_BIGINT_AS_STRING));

?>

以上例程会输出:

float(1.2345678901235E+19)
string(20) "12345678901234567890"

注释 ¶

Note:

The JSON spec is not JavaScript, but a subset of JavaScript.

Note:

In the event of a failure to decode, json_last_error() can be used to determine the exact nature of the error.

更新日志 ¶

版本 说明
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.

参见 ¶

  • json_encode() - 对变量进行 JSON 编码
  • json_last_error() - 返回最后发生的错误

原文地址:点击打开链接

PHP json_decode相关推荐

  1. 调用短信接口,先var_dump()看数据类型是object需要json_decode(json_encode( $resp),true)转换成array...

    返回的数据.先看类型,如果是object类型 先json_encode, 再json_decode,加true 转换成数组 $resp = $c->execute($req); var_dump ...

  2. php json_decode 后,数字对象转换成了 科学计数法 的解决方案

    php json_decode 后,数字对象转换成了 科学计数法 的解决方案 参考文章: (1)php json_decode 后,数字对象转换成了 科学计数法 的解决方案 (2)https://ww ...

  3. json_decode到数组

    我正在尝试将JSON字符串解码为数组,但出现以下错误. 致命错误:不能在第6行的C:\\ wamp \\ www \\ temp \\ asklaila.php中将stdClass类型的对象用作数组 ...

  4. json_decode的结果是null

    一.前言 突然发现一个接口出了问题,经过排查之后发现是json_decode($str,true)的问题,返回竟然是null.这个问题大家可能都碰到过,出现问题的原因就那么几种,再次记录一下吧 二.原 ...

  5. json_decode的结果为null,json_encode的结果为

    首先,楼主在写接口的时候,是用laravel查询数据库,然后对返回的结果进行:json_decode(json_encode($ret),true),,本以为能顺利的转化为json字符串输出的,结果却 ...

  6. php中对于json_decode()和json_encode()的使用方法笔记

    1.json_decode() json_decode  (PHP 5 >= 5.2.0, PECL json >= 1.2.0) json_decode - 对 JSON 格式的字符串进 ...

  7. php json_decode \ 反斜杠 转义 问题

    四斜杠 $j = '{"class":"app\\\\api"}'; dump(json_decode($j, true)); ["class&quo ...

  8. php 序列化储存和转化 json_encode() json_decode($q,true)

    序列化是将变量转换为可保存或传输的字符串的过程:反序列化就是在适当的时候把这个字符串再转化成原来的变量使用.这两个过程结合起来,可以轻松地存储和传输数据,使程序更具维护性. 例如:当需要数据库只有一个 ...

  9. PHP多种序列化/反序列化的方法 json_encode json_decode

    序列化是将变量转换为可保存或传输的字符串的过程:反序列化就是在适当的时候把这个字符串再转化成原来的变量使用.这两个过程结合起来,可以轻松地存储和传输数据,使程序更具维护性. 1. serialize和 ...

  10. PHP中的json_encode和json_decode

    1.json_decode() json_decode (PHP 5 >= 5.2.0, PECL json >= 1.2.0) json_decode - 对 JSON 格式的字符串进行 ...

最新文章

  1. kubernetes之一步一个坑
  2. SQL SERVER 2012修改数据库名称(包括 db.mdf 名称的修改)
  3. 红橙Darren视频引申 第一次写NDK项目(Android studio 4.1.1)
  4. (8)Verilog include 头文件使用路径(FPGA不积跬步101)
  5. 【flyway】flyway Migration checksum mismatch for migration
  6. 统一沟通-技巧-9-Lync 2010-Outlook 2010-自动配置-1-IT人员
  7. java project mybatis,Java使用Mybatis
  8. mysql i o开启_MySQL從屬I/O線程不運行。
  9. DeepRacer 资源合集
  10. 新人主播开播以后,碰到的各类问题和解决方法
  11. 怎么看台式计算机内存条,内存频率怎么看 教你怎么看内存条频率
  12. mysql pk uk ak,最重要的MySQL开发规范 全都在这了
  13. JavaScript内存溢出
  14. SQL Server部分知识的整理
  15. “结果导向型”思维的规则
  16. AI专业教您保姆级在暗影精灵8Windows11上本地部署实现AI绘画:Stable Diffusion(万字教程,多图预警)
  17. joblib.externals.loky.process_executor.BrokenProcessPool: A task has failed to un-serialize. Please
  18. 音频单元组件服务参考(Audio Unit Component Services Reference)
  19. 睡觉睡到自然醒,数钱数到手抽筋
  20. mac启动选项找不到linux,Mac升级10.10后开机引导不见了,无法进入Linux

热门文章

  1. OpenCV图像处理入门
  2. 机械原理葛文杰P47页牛头刨床运动分析代码,画图精致
  3. 文档在线预览(四)使用js前端实现word、excel、pdf、ppt 在线预览
  4. 写的另一款安全期避孕计算软件 (ISEX安全期计算) 强力推荐!
  5. TinyOS、NesC程序开发经验谈[转载]
  6. Android中okhttp原理详解
  7. [面试宝典] Linux常见命令及面试题
  8. 第三代移动通信技术(3G)
  9. java UUID的长度缩减
  10. 在SqlServer中,教你如何修改列名