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)
上一篇 2022年7月12日 下午11:36
下一篇 2022年7月12日 下午11:46


相关推荐

  • Subset笔记

    Subset笔记找全部子集 subset 习题链接算法 每次向 res 中的所有集合中添加下一个原集合元素 错误 每次都要 copyres 集合和 element 集合 res 集合 copy 是防止元素不停增加导致无限循环 element 集合 copy 是防止在原集合位置中做出修改 defsubset set res forelementin forset visitedinres x set visited

    2026年3月17日
    2
  • 数字证书理解(CA证书签名原理)[通俗易懂]

    数字证书理解(CA证书签名原理)[通俗易懂]目的为了防止中间人攻击和钓鱼基础概念(要求预先了解的知识概念)对称密钥体系(对称加密)和非对称密钥体系(非对称加密)都提供2份秘钥。公钥私钥是概念上的,发布出去的为公钥,留在手上的为私钥,实质上不存在公私钥区别。特殊的:在实际操作中,生成RSA(特别的:一种加密方式)密钥时会有两个秘钥,其中一份包含另一份的完整信息【此时默认命名为私钥】——->这就是为什么私钥可以推导出公…

    2022年6月1日
    46
  • 正確使用 SetCapture ReleaseCapture [譯]「建议收藏」

    正確使用 SetCapture ReleaseCapture [譯]「建议收藏」本文描述瞭如何正確處理自定義窗口和控件中的鼠標捕獲操作。原文鏈接: http://www.codeproject.com/Tips/127813/Using-SetCapture…correctly.aspx原作者: pasztorpisti轉載請註明出處:http://www.imoldman.com/2010/11/30/using-setcaptu…ture-corre

    2022年5月24日
    34
  • 单向链表之删除节点(C语言实现)「建议收藏」

    单向链表之删除节点(C语言实现)「建议收藏」链表的创建查看删除节点就是将某一节点从链中摘除。将待删节点与其前一节点解除联系(中间或尾部)或本阶段删除(头节点),并释放相应空间(free)。删除的第一步是找到要删除的节点,同链表查找,如果找不到或链表为空,提示未找到,找到后根据情况删除此节点。删除节点两种情况:第一个节点,后面节点。步骤:1、链表为空:不用删除2、链表不为空:先循环找要删除的节点1)找到了1>找

    2025年8月5日
    4
  • vue往数组中添加元素_vuejs给数组添加元素[通俗易懂]

    vue往数组中添加元素_vuejs给数组添加元素[通俗易懂]2016-09-13更新问题vuejs给数组添加元素代码是这样的:varvm=newVue({el:”#app”,data:{items:[{id:1,message:’Apple’,selected:false,num:1,price:5},{id:2,message:’Peach’,selected:true,num:1,price:10},{id:3,mes…

    2022年5月23日
    48
  • 提升开发效率:Modern Monaco语言服务器协议(LSP)配置指南

    提升开发效率:Modern Monaco语言服务器协议(LSP)配置指南

    2026年3月12日
    3

发表回复

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

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