PHP json_decode[通俗易懂]

PHP json_decode[通俗易懂]json_decode(PHP5>=5.2.0,PECLjson>=1.2.0)json_decode — 对JSON格式的字符串进行编码说明 ¶mixed json_decode ( string $json [, bool $assoc =false [, int $depth =512 [, int $options =0 

大家好,又见面了,我是你们的朋友全栈君。

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 truefalse 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($jsontrue));

?>

以上例程会输出:

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(
        
=> 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(43, -1) as $depth) {

    
var_dump(json_decode($jsontrue$depth));
    echo 
'Last error: '$json_errors[json_last_error()], PHP_EOLPHP_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_NONE

NULL
Last error: JSON_ERROR_DEPTH

Example #5 json_decode() of large integers

<?php
$json 
'12345678901234567890';

var_dump(json_decode($json));
var_dump(json_decode($jsonfalse512JSON_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.

参见 ¶

原文地址:点击打开链接

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/158324.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • java基础题目_40道java基础练习题,你会卡在哪道题?

    java基础题目_40道java基础练习题,你会卡在哪道题?1表达式5.3+(int)(8.5+4.6)/3%4的值是(5.3)。2表示圆周率的常量是(Math.PI)。3使用关键字(boolean)来定义逻辑变量。4执行语句inta,b,c;a=1;b=3;c=(a+b>3?++a:b++);后,b的值为(3)。5表示换行符的字符常量是(”)。6若reader是一个Scanner类的对象,读取一个字符串(以…

    2022年7月8日
    22
  • 石器时代服务器架设教程_石器时代gm命令

    石器时代服务器架设教程_石器时代gm命令本文只作学习研究之用,任何人不得非法使用。Linux系统架设石器私服概述首要条件,安装Linux版系统,建议CentOS4或者5工具:SSHvncforLinuxandwinxamppforLinux主机在眼前的就不说了,首先说的是如果你租了服务器,机房给你装好系统你自己还需要一些工具再进Linux一般Linux系统用户名默认为root服务商会给你一个初始密码ssh安装在自己w…

    2022年9月26日
    3
  • js动画requestAnimationFrame详解「建议收藏」

    js动画requestAnimationFrame详解「建议收藏」看这篇文章之前我希望你会用setTimeout做简单的动画,也就是利用递归来代替setInterval做动画。requestAnimationFrame()他的作用就是代替定时器做更加流畅高性能的动画,做可以匹配设备刷新率的动画,他解决了定时器做动画时间间隔不稳定的问题(也就是解决定时器做动画不流畅的问题)。他的用法与setTimeout差不多。与setTimeout一样的是都会返回一个唯一标识,setTimeout可以通过clearTImeout()关闭定时器。那么requestAnimatio.

    2022年8月31日
    4
  • 结合企业实例谈IT规划过程

    结合企业实例谈IT规划过程信息化规划过程模型信息化规划架构模型信息化架构示例

    2025年8月9日
    2
  • 分享一个年化15%以上的无风险套利机会「建议收藏」

    分享一个年化15%以上的无风险套利机会「建议收藏」更多精彩内容,欢迎关注公众号:数量技术宅,也可添加技术宅个人微信号:sljsz01,与我交流。数字货币期现套利原理数字货币市场,是近期大家关注度相当高的一个市场。在这个市场中,存在着现货交易(数字

    2022年8月6日
    7
  • (一)easyUI之第一个demo

    (一)easyUI之第一个demo一、下载官网下载:http://www.jeasyui.net/download/同时并下载官方中文API文档。解压后的目录结构:二、第一个demo1新建工程并导入包1新建工程并导

    2022年7月2日
    24

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号