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


相关推荐

  • 错误: 在类中找不到 main 方法, 请将 main 方法定义为:public static void main(String[] args)否则 JavaFX 应用程序类必须扩展javafx.ap

    错误: 在类中找不到 main 方法, 请将 main 方法定义为:public static void main(String[] args)否则 JavaFX 应用程序类必须扩展javafx.ap最近在使用eclipse编写java程序时遇到这样一个问题:错误在类中找不到main方法,请将main方法定义为publicstaticvoidmain(String[]args)否则JavaFX应用程序类必须扩展javafx.application.Application看到这样的问题让我一头雾水,因为main方法已经写出解决这个问题可以点开eclipse-&amp;gt…

    2022年5月31日
    42
  • navicat15万能激活码(最新序列号破解)

    navicat15万能激活码(最新序列号破解),https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月19日
    586
  • 【剑指offer】删除字符也出现在一个字符串

    【剑指offer】删除字符也出现在一个字符串

    2022年1月3日
    45
  • webstorm 插件安装

    webstorm 插件安装1.打勾的表示已经安装2.没有安装的插件,可以在plugins搜索,在右边搜索结果里点install,然后重启webstorm3.这里有常用插件http://blog.csdn.net/xs20691718/article/details/52269027转载于:https://www.cnblogs.com/linsx/p/7770623.html…

    2025年10月11日
    4
  • 大数据分析那点事

    写在前文,首先声明博主对数据分析领域也在不断学习当中,文章中难免可能会出现一些错误,欢迎大家及时指正,博主在此之前也曾对不同量级、不同领域的数据进行过分析,但是在过程中总是感觉有许多困惑,即自己也会问自己?自己分析的是否全面,是否有价值,从哪些方面出发?对于这些问题博主做了思考。归根到底还是在理论上,在阅读了相关的专业书籍和材料的基础上总结出本文,希望能给大家带来收获,同时由于内容过多,计划分三次完成全部内容,同时如果大家感觉对自己有帮助的话,记得收藏,博主会不断完善本文的缺陷和不足,希望真正能给大家带来收

    2022年4月8日
    50
  • pychram2021.11.3激活【2021免费激活】

    (pychram2021.11.3激活)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~9ZHR…

    2022年3月28日
    59

发表回复

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

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