WissensdatenbankPrüfungszentrum
DEDeutsch
Sprache wählen13

CKTIS / PHP

JsonException

Fängt Fehler einer JSON-Operation mit JSON_THROW_ON_ERROR ab.

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

Syntax

catch (JsonException $error) { /* inspect JSON error */ }

Fängt Fehler einer JSON-Operation mit JSON_THROW_ON_ERROR ab. Der Code nennt die Fehlerart, etwa JSON_ERROR_SYNTAX. Gültiges JSON null dekodiert normal und bleibt von defekter Eingabe unterscheidbar. Standardflags aktivieren diesen Ausnahmebetrieb nicht.

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]

Beispiele

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

Ergebnis

[true,null]

Quellen