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 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.

范例 ¶

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_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($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.

参见 ¶

原文地址:点击打开链接

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

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

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


相关推荐

  • eclipse安装教程(win10)

    eclipse安装教程(win10)1.官网下载下载链接:http://www.eclipse.org/downloads/2.运行运行后有很多版本可供选择EclipseIDEforJavaDevelopers是为java开发的EclipseIDEforJavaEEDevelopers是为J2EE开发的EclipseforRCP/Plug-inDevelopers是…

    2022年6月8日
  • keyvaluepair_【转】KeyValuePair用法「建议收藏」

    keyvaluepair_【转】KeyValuePair用法「建议收藏」KeyValuePair用法(转)C#KeyValuePair的用法。结构体,定义可设置或检索的键/值对。也就是说我们可以通过它记录一个键/值对这样的值。比如我们想定义一个ID(int类型)和Name(string类型)这样的键/值对,那么可以这样使用。//////设置键/值对//////privateKeyValuePairSetKeyValuePair(){intintKey…

    2022年7月26日
  • Mac如何修改host文件「建议收藏」

    Mac如何修改host文件「建议收藏」首先开启一个文件夹,点击上方【前往】-&gt;【前往文件夹】。 输入“/private/etc/hosts”,点击【前往】。 自动开启“etc”文件夹,找到【hosts文件】,并将其拉到桌面上才能修改桌面上的hosts文件。 “右键”桌面上hosts文件,选择【打开文件的应用程序】,使用【文字编辑】开启。 开启编辑hosts文件。 编辑完后就把桌面上的…

    2022年10月12日
  • oracle 转number日期,oracle number型日期转date型日期

    oracle 转number日期,oracle number型日期转date型日期在搞数据库时,发现有这样的一个字段,类型是NUMBER(38),查看了一下里面的数据,都是这样的,1323957678114132132699429513221293068041322129716220猜到应该是1970年1月1号0点0分距现在的毫秒值。解决办法是写一个转换函数:createorreplacefunctionnum_to_date(in_numberNUMBER)ret…

    2022年7月24日
  • 教你保存在线视频文件「建议收藏」

      当你看到很精彩的视频你想不想把他保存起来以后继续欣赏呢?或者是做成MP4格式放到手机里??但是目前绝大部分的视频网站由于版权、带宽等原因不提供视频下载服务,甚至想方设法把这些视频资源藏起来。所以你无法把它们保存到自己的电脑上。我们要怎么样才能把别人的视频文件保存到自己的电脑上呢?下面教你几招保存视频文件,让我们来突破封锁,把在线视频搬回家,想看就看!  一、WMV、ASF…

    2022年4月6日
  • python解压bz2文件命令,在Python中解压缩.bz2文件

    python解压bz2文件命令,在Python中解压缩.bz2文件So,thisisaseeminglysimplequestion,butI’mapparentlyveryverydull.Ihavealittlescriptthatdownloadsallthe.bz2filesfromawebpage,butforsomereasonthedecompressingofthatfile…

    2022年5月31日

发表回复

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

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