CKTIS / PHP
JsonException
Intercetta gli errori delle operazioni JSON con JSON_THROW_ON_ERROR.
Versione: PHP 8.4; repository runtime, production runtime not attested
Sintassi
catch (JsonException $error) { /* inspect JSON error */ }Intercetta gli errori delle operazioni JSON con JSON_THROW_ON_ERROR. Il codice indica il tipo di errore, ad esempio JSON_ERROR_SYNTAX. Il JSON null valido si decodifica normalmente e si distingue dai dati malformati. Le opzioni predefinite non attivano l’eccezione.
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]
Esempi
<?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)]);
Risultato
[true,null]