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


相关推荐

  • rolling在舞蹈里是什么意思_机械舞和街舞有啥区别

    rolling在舞蹈里是什么意思_机械舞和街舞有啥区别原标题:这,就是街舞中的那些“Swag”十足的舞蹈类型,你了解吗?世界越来越小了,人们越靠越近,视野越来越广,局限于逼仄的小空间的时代已经一去不复返了。现在,一推门,迎面就是全世界。自去年开始,嘻哈文化开始通过综艺节目进入公众视野(中国有嘻哈),这种来自大洋彼岸的“陌生文化”随即引起了广泛的关注。尽管作为“第一个吃螃蟹的人”,《中国有嘻哈》的最终结果不尽如人意,但却为后来的综艺制作人提供了全新的视…

    2025年5月28日
    0
  • android之存储篇_存储方式总览

    作为一个完成的应用程序,数据存储操作是必不可少的。因此,Android系统一共提供了四种数据存储方式。分别是:SharePreference、SQLite、Content Provider和File。由于Android系统中,数据基本都是私有的的,都是存放于“data/data/程序包名”目录下,所以要实现数据共享,正确方式是使用Content Provider。  SQLite: SQLit

    2022年3月10日
    40
  • linux安装siege

    linux安装siegesiege安装笔记本文介绍centos和ubuntu安装方法centos安装下载:[root@siege-4.0.4]#wgethttp://download.joedog.org/siege/siege-4.0.4.tar.gz解压:[root@siege-4.0.4]#tarzxvfsiege-4.0.4.tar.gz打开解压包:…

    2025年7月10日
    0
  • rtsp 获取视频流 java_OpenCV – 如何捕获rtsp视频流

    rtsp 获取视频流 java_OpenCV – 如何捕获rtsp视频流例如,我们有工作rtsp流测试像:“rtsp://184.72.239.149/vod/mp4:BigBuckBunny_115k.mov”(它在发布这篇文章的时候工作)现在我想在openCV中捕获这个视频流(opencv2.4.7/2.4.8)我的代码完全适用于本地电影文件,但当我尝试捕获rtsp时,我得到的信息如下:“无法读取电影文件RTSP://184.72.239.149/vod/m…

    2022年10月17日
    0
  • 阿里云服务器配置ssl证书_阿里云服务器配置选择

    阿里云服务器配置ssl证书_阿里云服务器配置选择阿里云配置SSL证书证书申请概览![在这里插入图片描述](https://img-blog.csdnimg.cn/20210511153723521.png)申请配置证书申请概览申请两种方式:进入阿里云控制台页面->安全(模块/菜单)->SSL证书;在阿里云搜索框中进行搜索ssl证书点击进入;购买证书需要注意的是已过期的证书是没有到期新购操作的,只能重新购买。Symantec免费SSL证书我们选择Symantec免费型DVSSL,不花钱0元就可以购买。这个免

    2022年10月3日
    0
  • 推荐系统中传统模型——LightGBM + FFM融合

    推荐系统中传统模型——LightGBM + FFM融合之前比较相关的文章:推荐系统中传统模型——LightGBM+LR融合python-机器学习lightgbm相关实践1深入FFM原理与实践来自美团技术团队的,深入FFM原理与实践FM和FFM模型是最近几年提出的模型,凭借其在数据量比较大并且特征稀疏的情况下,仍然能够得到优秀的性能和效果的特性,屡次在各大公司举办的CTR预估比赛中获得不错的战绩。美团技术团队在搭建DSP的过程中,探索并使用了FM和FFM模型进行CTR和CVR预估,并且取得了不错的效果。经过One-Hot编码之后,大部分

    2022年5月27日
    34

发表回复

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

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