downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

Judy> <json_last_error_msg
[edit] Last updated: Fri, 17 May 2013

view this page in

json_last_error

(PHP 5 >= 5.3.0)

json_last_errorGibt den letzten aufgetretenen Fehler zurück

Beschreibung

int json_last_error ( void )

Gibt (sofern vorhanden) den letzten Fehler zurück, der beim letzten Parsen von JSON aufgetreten ist.

Parameter-Liste

Diese Funktion hat keine Parameter.

Rückgabewerte

Gibt einen Integer zurück, der Wert kann eine der folgenden Konstanten sein:

JSON-Fehlercodes
Konstante Bedeutung
JSON_ERROR_NONE Kein Fehler aufgetreten.
JSON_ERROR_DEPTH Die maximale Stacktiefe wurde überschritten.
JSON_ERROR_CTRL_CHAR Steuerzeichenfehler, möglicherweise unkorrekt kodiert.
JSON_ERROR_SYNTAX Syntaxfehler.

Beispiele

Beispiel #1 json_last_error()-Beispiel

<?php
// Ein gültiger JSON-String
$json[] = '{"Organisation": "PHP-Dokumentationsteam"}';

// Ein ungültiger JSON-String, der einen Syntaxfehler hervorruft,
// in diesem Fall werden ' anstelle von " als Anführungszeichen verwendet
$json[] = "{'Organisation': 'PHP-Dokumentationsteam'}";


foreach(
$json as $string)
{
 echo 
'Dekodiere: ' $string;
 
json_decode($string);

 switch(
json_last_error())
 {
  case 
JSON_ERROR_DEPTH:
   echo 
' - Maximale Stacktiefe überschritten';
  break;
  case 
JSON_ERROR_CTRL_CHAR:
   echo 
' - Unerwartetes Steuerzeichen gefunden';
  break;
  case 
JSON_ERROR_SYNTAX:
   echo 
' - Syntaxfehler, ungültiges JSON';
  break;
  case 
JSON_ERROR_NONE:
   echo 
' - Keine Fehler';
  break;
 }

 echo 
PHP_EOL;
}
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

Decoding: {"Organisation": "PHP-Dokumentationsteam"} - Keine Fehler
Decoding: {'Organisation': 'PHP-Dokumentationsteam'} - Syntaxfehler, ungültiges JSON

Siehe auch



add a note add a note User Contributed Notes json_last_error - [2 notes]
up
12
jimmetry at gmail dot com
1 year ago
While this can obviously change between versions, the current error codes are as follows:

0 = JSON_ERROR_NONE
1 = JSON_ERROR_DEPTH
2 = JSON_ERROR_STATE_MISMATCH
3 = JSON_ERROR_CTRL_CHAR
4 = JSON_ERROR_SYNTAX
5 = JSON_ERROR_UTF8

I'm only posting these for people who may be trying to understand why specific JSON files are not being decoded. Please do not hard-code these numbers into an error handler routine.
up
-2
lior at mytopia dot com
4 years ago
For those of you who prefer a more object oriented approach (as I do), here is a fairly simple wrapper that handles errors using exceptions:

<?php

class JSON
{
    public static function
Encode($obj)
    {
        return
json_encode($obj);
    }
   
    public static function
Decode($json, $toAssoc = false)
    {
       
$result = json_decode($json, $toAssoc);
        switch(
json_last_error())
        {
            case
JSON_ERROR_DEPTH:
               
$error ' - Maximum stack depth exceeded';
                break;
            case
JSON_ERROR_CTRL_CHAR:
               
$error = ' - Unexpected control character found';
                break;
            case
JSON_ERROR_SYNTAX:
               
$error = ' - Syntax error, malformed JSON';
                break;
            case
JSON_ERROR_NONE:
            default:
               
$error = '';                   
        }
        if (!empty(
$error))
            throw new
Exception('JSON Error: '.$error);       
       
        return
$result;
    }
}

?>

 
show source | credits | stats | sitemap | contact | advertising | mirror sites