CKTIS / PHP
json_decode()
Lee JSON UTF-8 como valores de PHP.
Versión: PHP 8.4; repository runtime, production runtime not attested
Sintaxis
json_decode(string $json, ?bool $associative = null, int $depth = 512, int $flags = 0): mixedLee JSON UTF-8 como valores de PHP. Un objeto da stdClass por defecto, associative=true crea un array y null sigue JSON_OBJECT_AS_ARRAY. Tanto JSON null válido como un error sintáctico dan null por defecto. JSON_THROW_ON_ERROR distingue el error con una excepción.
Firma
json_decode(string $json, ?bool $associative = null, int $depth = 512, int $flags = 0): mixed
Salida del ejemplo
["stdClass",2,null,"JsonException"]
Ejemplos
<?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]);
Resultado
["stdClass",2,null,"JsonException"]