成人精品一区二区三区中文字幕-成人精品一区二区三区-成人精品一级毛片-成人精品亚洲-日本在线视频一区二区-日本在线视频免费

導航首頁 ? 技術教程 ? PHP:json_decode()的用法_JSON函數
全站頭部文字 我要出現在這里
PHP:json_decode()的用法_JSON函數 758 2023-12-15   

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)

返回值

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.

參見

json_encode() - 對變量進行 JSON 編碼 json_last_error() - 返回最后發生的錯誤


主站蜘蛛池模板: 456电影456重口味是免费观看| 《平凡之路》电影| 赤胆情| angelababy婚礼大作战| 结婚十年电视剧| 保坂尚辉| 黄色网址视频| 分家协议| 武林外传豆瓣| 初恋在线观看| 速度与激情:特别行动 电影| 廊桥遗梦演员表| 文史茶馆| 徐乃麟个人简历| abo血型鉴定实验报告| 苑琼丹三级| 女生宿舍2在线看| 水咲ローラ| 乙亥北行日记| 李美琪主演的电影| juliet stevenson| 健康中国科普先行直播回放| 西海情歌原歌词全文| 老男人gay同性gay做受| 金枝玉叶电视剧免费观看| 少年歌行第三季| 科室对分级护理落实情况检查记录| 在线观看亚洲免费视频| 少女椿电影| 姐妹兄弟演员表| 草刈正雄| 如来神掌电视剧| 我爱你再见分集剧情介绍| 徐贤电影| 100张照片| 东方卫视节目表| 韩国女大学生电影| 电影百鸟朝凤免费完整版| 浙江卫视今天节目单| 菲律宾电影甜蜜宝贝| 老妇勾搭少年|

!!!站長長期在線接!!!

網站、小程序:定制開發/二次開發/仿制開發等

各種疑難雜癥解決/定制接口/定制采集等

站長微信:lxwl520520

站長QQ:1737366103