json_decode

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

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

Report a bug

说明

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

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

Report a bug

参数

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)

Report a bug

返回值

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

Report a bug

范例

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"

Report a bug

注释

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.

json_decode用法相关推荐

  1. PHP函数json_decode的用法,PHP json_decode()用法及代码示例

    json_decode()函数是PHP中的内置函数,用于解码JSON字符串.它将JSON编码的字符串转换为PHP变量. 用法: json_decode( $json, $assoc = FALSE, ...

  2. PHP中json_encode与json_decode用法

    一.json_encode() 对变量进行JSON编码, 语法: json_encode ( $value [, $options = 0 ] ) 注意:1.$value为要编码的值,且该函数只对UT ...

  3. PHP json_decode 用法

    阅读目录 json_decode 打印格式 解决报错 json_encode JSON_UNESCAPED_UNICODE json_decode 打印格式 $json = '{"a&quo ...

  4. php中json_encode和json_decode的用法

    1.json_encode基本用法:数组转字符串 <?php $arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5); ...

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

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

  6. php中json_decode()和json_encode()的使用方法

    php中json_decode()和json_encode()的使用方法 json_decode对JSON格式的字符串进行编码而json_encode对变量进行 JSON 编码,需要的朋友可以参考下 ...

  7. json_decode()和json_encode()区别----2015-0929

    json_decode对JSON格式的字符串进行编码而json_encode对变量进行 JSON 编码,需要的朋友可以参考下 1.json_decode() json_decode (PHP 5 &g ...

  8. php 调用微信收货地址,php微信自动获取收货地址api用法实例详解

    这篇文章主要介绍了php版微信自动获取收货地址api用法,结合实例形式分析了php版微信API接口调用与使用技巧,需要的朋友可以参考下 微信公众平台现在是越来越强大了,我们可以通过各种api接口来与平 ...

  9. php json_decode utf8,关于utf 8:PHP json_encode json_decode UTF-8

    如何将包含国际字符的JSON编码字符串保存到数据库中,然后在浏览器中解析解码后的字符串? $string ="très agréable"; // to the database ...

最新文章

  1. 浅显易懂 Makefile 入门 (03)— 目标文件搜索(VPATH 和 vpath 的区别和使用)、隐含规则
  2. 怎么两边同时取ln_脏辫发型怎么编编发教程图解简单易学!
  3. 训练日志 2018.11.28
  4. Thingsboard 3.1.0 - UI修改
  5. 【漏洞学习——任意文件上传】LAMP兄弟连旗下猿代码存在任意上传文件漏洞
  6. 使用python进行数据清洗常用的库_用于格式化和数据清理的便捷Python库
  7. zdragon 厚积薄发(博客)
  8. 初探Java设计模式4:JDK中的设计模式
  9. 升级到JUnit5的7个理由
  10. IDEA 如何打开一个jsp文件?
  11. ubuntu IPV6及作为路由分配【笔记】
  12. 虚拟机屏幕自适应问题
  13. 2017.2.10考试总结2017冬令营
  14. 技术管理者应具备哪些能力
  15. 【挑战程序设计】- 2.5 图论(最短路、最小生成树)
  16. VUE将两条数据组合成一条数据
  17. Spark入门实战系列--9.Spark图计算GraphX介绍及实例
  18. Mac OS 电脑恢复出厂设置之详细操作
  19. 大学学计算机应该选哪个专业呢?
  20. 【图像识别】基于HSV和RGB模型水果分类matlab源码含 GUI

热门文章

  1. binhemedia.cn 联系我们_【图片】想在邵阳市打个水井找谁好,附近专业快速钻井唐师傅联系电话,【邵阳生活吧】...
  2. mysql配置master_mysql 主从配置(master/slave)
  3. Dw序号列表如何通过html语言加,使用DW软件实现html编码转换的详细步骤
  4. python爬虫模拟浏览器的两种方法_python爬虫模拟浏览器访问-User-Agent过程解析
  5. Date对象 IOS踩坑
  6. VUE2)vue-devtools的安装与使用
  7. Idea加快开发的10个技巧
  8. 在MAC下怎样用SSH连接远程LINUXserver
  9. BZOJ2612 : [Poi2003]Sums
  10. drawRect方法在UIImageView的派生类中不被调用