(PHP 5 >= 5.2.0, PHP 7, PECL json >= 1.2.0)
json_decode — JSON 文字列をデコードする
$json
[, bool $assoc = false
[, int $depth = 512
[, int $options = 0
]]] ) : mixedJSON エンコードされた文字列を受け取り、それを PHP の変数に変換します。
json
デコード対象となる json 文字列。
この関数は UTF-8 でエンコードされた文字列でのみ動作します。
注意:
PHP の実装は、 » RFC 7159 の JSON のスーパーセットです。
assoc
TRUE の場合、返されるオブジェクトは連想配列形式になります。
depthユーザー指定の再帰の深さ。
options
JSON デコードオプションのビットマスクです。
Currently there are two supported
options. The first is JSON_BIGINT_AS_STRING that
allows casting big integers to string instead of floats which is the
default. The second option is JSON_OBJECT_AS_ARRAY
that has the same effect as setting assoc to
TRUE.
json でエンコードされたデータを、適切な PHP の型として返します。
true、false および
null はそれぞれ TRUE、FALSE
そして NULL として返されます。
json のデコードに失敗したり
エンコードされたデータが再帰制限を超えているなどの場合、NULL を返します。
例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)
}
例2 無効なオブジェクトプロパティへのアクセス
オブジェクトの中にある、 PHP の命名規約では使えない文字 (ハイフンなど) を含む要素にアクセスするには、 要素名を波括弧とアポストロフィで囲みます。
<?php
$json = '{"foo-bar": 12345}';
$obj = json_decode($json);
print $obj->{'foo-bar'}; // 12345
?>
例3 json_decode() でのありがちな間違い
<?php
// 以下の文字列は JavaScript としては有効ですが JSON としては無効です
// 名前と値はダブルクォートで囲む必要があります。
// シングルクォートは使えません
$bad_json = "{ 'bar': 'baz' }";
json_decode($bad_json); // null
// 名前をダブルクォートで囲まなければなりません
$bad_json = '{ bar: "baz" }';
json_decode($bad_json); // null
// 最後にカンマをつけてはいけません
$bad_json = '{ bar: "baz", }';
json_decode($bad_json); // null
?>
例4 depth エラー
<?php
// データをエンコードします
$json = json_encode(
array(
1 => array(
'English' => array(
'One',
'January'
),
'French' => array(
'Une',
'Janvier'
)
)
)
);
// エラーを定義します
$constants = get_defined_constants(true);
$json_errors = array();
foreach ($constants["json"] as $name => $value) {
if (!strncmp($name, "JSON_ERROR_", 11)) {
$json_errors[$value] = $name;
}
}
// さまざまな深さのエラーを表示します
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
例5 json_decode() で大きな整数値を扱う例
<?php
$json = '{"number": 12345678901234567890}';
var_dump(json_decode($json));
var_dump(json_decode($json, false, 512, JSON_BIGINT_AS_STRING));
?>
上の例の出力は以下となります。
object(stdClass)#1 (1) {
["number"]=>
float(1.2345678901235E+19)
}
object(stdClass)#1 (1) {
["number"]=>
string(20) "12345678901234567890"
}
注意:
JSON の仕様は JavaScript そのものではなく、JavaScript のサブセットです。
注意:
デコードに失敗した場合は、json_last_error() を使用すればエラーの正確な状態を知ることができます。
| バージョン | 説明 |
|---|---|
| 7.1.0 | An empty JSON key ("") can be encoded to the empty object property instead of using a key with value _empty_. |
| 7.0.0 | Rejected RFC 7159 incompatible number formats - top level (07, 0xff, .1, -.1) and all levels ([1.], [1.e1]) |
| 7.0.0 | An empty PHP string or value that after casting to string is an empty string (NULL, FALSE) results in JSON syntax error. |
| 5.6.0 | true、 false および null は、すべて小文字のものだけを有効な値として受け付けるようになりました。 大文字が含まれる場合は警告が発生します。 |
| 5.4.0 |
options パラメータが追加されました。
|
| 5.3.0 | オプションの depth が追加されました。デフォルトの再帰の深さが 128 から 512 に増えました。 |
| 5.2.3 | ネストの制限が 20 から 128 に拡張されました。 |
| 5.2.1 | 基本型の JSON デコードに対応しました。 |