Base di conoscenzaCentro esami
ITItaliano
Scegli la lingua13

CKTIS / PHP

json_decode()

Legge JSON UTF-8 come valori PHP.

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

Sintassi

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

Legge JSON UTF-8 come valori PHP. Un oggetto diventa stdClass per default, associative=true crea un array e null segue JSON_OBJECT_AS_ARRAY. Il JSON null valido e un errore sintattico danno null per default. JSON_THROW_ON_ERROR distingue l’errore con un’eccezione.

Firma

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

Output dell’esempio

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

Esempi

<?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]);

Risultato

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

Fonti