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

search for in the

Misc.> <json_encode
Last updated: Fri, 14 Aug 2009

view this page in

json_last_error

(PHP 5 >= 5.3.0)

json_last_errorRetourne la dernière erreur JSON

Description

int json_last_error ( void )

json_last_error() retourne la dernière erreur, s'il y en a eu, survenue lors de la dernière analyse JSON.

Liste de paramètres

Cette fonction ne contient aucun paramètre.

Valeurs de retour

Retourne une des constantes suivantes :

Codes d'erreur JSON
Constante Signification
JSON_ERROR_NONE Aucune erreur n'est survenue
JSON_ERROR_DEPTH La profondeur maximale de la pile a été atteinte
JSON_ERROR_CTRL_CHAR Erreur lors du contrôle des caractères ; probablement un encodage incorrect
JSON_ERROR_SYNTAX Erreur de syntaxe

Exemples

Exemple #1 Exemple avec json_last_error()

<?php
// Une chaîne JSON valide
$json[] = '{"Organisation": "Équipe de Documentation PHP"}';

// Une chaîne json invalide qui va générer une erreur de syntaxe,
// ici, utilisation de ' au lieu de "
$json[] = "{'Organisation': 'Équipe de Documentation PHP'}";


foreach(
$json as $string)
{
    echo 
'Décodage : ' $string;
    
json_decode($string);

    switch(
json_last_error())
    {
        case 
JSON_ERROR_DEPTH:
            echo 
' - Profondeur maximale atteinte';
        break;
        case 
JSON_ERROR_CTRL_CHAR:
            echo 
' - Erreur lors du contrôle des caractères';
        break;
        case 
JSON_ERROR_SYNTAX:
            echo 
' - Erreur de syntaxe ; JSON malformé';
        break;
        case 
JSON_ERROR_NONE:
            echo 
' - Aucune erreur';
        break;
    }

    echo 
PHP_EOL;
}
?>

L'exemple ci-dessus va afficher :

Décodage : {"Organisation": "Équipe de Documentation PHP"} - Aucune erreur
Décodage : {'Organisation': 'Équipe de Documentation PHP'} - Erreur de syntaxe ; JSON malformé

Voir aussi



add a note add a note User Contributed Notes
json_last_error
lior at mytopia dot com
09-Mar-2009 02:17
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;
    }
}

?>

Misc.> <json_encode
Last updated: Fri, 14 Aug 2009
 
 
show source | credits | sitemap | contact | advertising | mirror sites