CKTIS / PHP
JsonException
Captura errores de operaciones JSON con JSON_THROW_ON_ERROR.
Versión: PHP 8.4; repository runtime, production runtime not attested
Sintaxis
catch (JsonException $error) { /* inspect JSON error */ }Captura errores de operaciones JSON con JSON_THROW_ON_ERROR. El código identifica el error, por ejemplo JSON_ERROR_SYNTAX. JSON null válido se decodifica normalmente y se distingue de una entrada incorrecta. Las opciones predeterminadas no activan la excepción.
try { json_decode("{",true,512,JSON_THROW_ON_ERROR); }
catch(JsonException $e){$syntax=$e->getCode()===JSON_ERROR_SYNTAX;}
echo json_encode([$syntax,json_decode("null",true,512,JSON_THROW_ON_ERROR)]);
[true,null]
Ejemplos
<?php
try { json_decode("{",true,512,JSON_THROW_ON_ERROR); }
catch(JsonException $e){$syntax=$e->getCode()===JSON_ERROR_SYNTAX;}
echo json_encode([$syntax,json_decode("null",true,512,JSON_THROW_ON_ERROR)]);
Resultado
[true,null]