Znalostní bázeCentrum zkoušek
CSČeština
Vybrat jazyk13

CKTIS / PHP

json_decode()

Načte JSON v UTF-8 jako hodnoty PHP.

Verze: PHP 8.4; repository runtime, production runtime not attested

Syntaxe

json_decode(string $json, ?bool $associative = null, int $depth = 512, int $flags = 0): mixed

Načte JSON v UTF-8 jako hodnoty PHP. Objekt běžně dá stdClass, associative=true vytvoří pole a null se řídí JSON_OBJECT_AS_ARRAY. Platné JSON null i syntaktická chyba běžně dají null. JSON_THROW_ON_ERROR rozliší chybu výjimkou.

Signatura

json_decode(string $json, ?bool $associative = null, int $depth = 512, int $flags = 0): mixed

Výstup příkladu

["stdClass",2,null,"JsonException"]

Příklady

<?php
$object = json_decode("{\"x\":2}"); $map = json_decode("{\"x\":2}",true);
try { json_decode("[",true,512,JSON_THROW_ON_ERROR); } catch (JsonException $e) { $bad = get_class($e); }
echo json_encode([get_class($object),$map["x"],json_decode("null"),$bad]);

Výsledek

["stdClass",2,null,"JsonException"]

Zdroje