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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • io电平转换芯片_一般plc均配置io电平转换

    io电平转换芯片_一般plc均配置io电平转换我们在使用ic2总线时,常常因为3.3v单片机与5v外围器件之间电压不匹配而需要进行电平转换,特将次转换电路记录一下,防止以后寻找时麻烦,同时此电路可以应用于大多数电平转换电路,如3.3V转12V也同样可以使用电路原理如下原理分析3.3V控制5V5V控制3.3V也可以用在3.3v单片机输出5v的TTL电平,时进行应用。可以使用BSS123代替2N7002特此记录,anlog…

    2022年8月30日
    3
  • vector初始化与清空[通俗易懂]

    vector初始化与清空[通俗易懂]vector初始化与清空vector初始化vector<int>v;如果直接进行访问v[i]会报错解决方案:可以使用v.resize(n)或者v.resize(n,m)来初始化,前者是使用n个0来初始化,后者是使用n个m来初始化。vector清空//1.clear,清空元素,但不回收空间v.clear();//2.swap,清空元素并回收内存vector<int>().swap

    2025年12月8日
    3
  • PKI体系框架「建议收藏」

    PKI体系框架「建议收藏」  PKI是PublicKeyInfrastructure的缩写,主要功能是绑定整数持有者的身份和相关秘钥对(通过为公钥及相关的用户身份信息签发数字证书),为用户提供方便的证书申请、证书作废、证书获取、证书状态查询的途径,并且利用数字证书及相关的各种服务(证书发布、黑名单机制、时间戳服务等)实现通信中各实体的身份认证、完整性、抗抵赖性和保密性。  根据数字证书格式和密钥管理方式的不同,PK…

    2022年8月22日
    8
  • AngularJS简介

    AngularJS简介AngularJS简介AngularJS是一个JavaScript矿建,他是一个JavaSscript编写的库。可以通过&lt;script&gt;标签添加到HTML页面。AngularJS是通过指令扩展了HTML,且通过表达式绑定数据到HTML。地址:&lt;scriptsrc="http://apps.bdimg.com/libs/angular.js/1.4.6/angul…

    2022年7月25日
    7
  • MySQL安装(详细,适合小白)

    MySQL安装(详细,适合小白)MySQL安装一、mysql安装包下载二、配置my.ini文件三、初始化MySQL四、可能遇到的错误操作一、mysql安装包下载官网下载地址:mysql安装包下载如图所示:二、配置my.ini文件解压后的文件尽量不要放在C盘(内存小),解压后如下图所示在上图所示梗目录下配置my.ini文件[mysqld]#设置3306端口port=3306[mysqld]#设置3306端口port=3306#设置mysql的安装目录(存放地址可以更改)basedir=E:\My

    2022年6月6日
    41
  • 微软校招试题

    把微软的这个笔试题贴出来,纯粹是为了方便大家学习交流,相信微软不会那么小气来追究我的责任吧。确实觉得微软出的这些题都不错,虽然只有20道选择,但是考察的面很全,数据结构,网络,算法,操作系统,概率等等

    2021年12月25日
    50

发表回复

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

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