PHP JSON Fonctions
Introduction à PHP JSON
L'extension JSON implémente le format d'échange de données JavaScript Object Notation.
Dans PHP 5, le décodage est géré par un analyseur basé sur le JSON_checker de Douglas Crockford.
PHP 7 dispose d'un nouvel analyseur amélioré, spécifiquement écrit pour PHP et sous licence PHP.
Installation
À partir de PHP 5.2.0, les fonctions JSON sont activées par défaut. Aucune installation n'est nécessaire pour utiliser ces fonctions.
Fonctions PHP JSON
| Fonction | Description |
|---|---|
| json_decode() | Décode une chaîne JSON |
| json_encode() | Encode une valeur au format JSON |
| json_last_error() | Renvoie la dernière erreur survenue |
| json_last_error_msg() | Renvoie la chaîne d'erreur de la dernière appel à json_encode() ou json_decode() |
Constantes JSON prédéfinies en PHP
| Constante | Type | Description |
|---|---|---|
| JSON_ERROR_NONE | Entier | Aucune erreur n'est survenue |
| JSON_ERROR_DEPTH | Entier | La profondeur maximale de la pile a été dépassée |
| JSON_ERROR_STATE_MISMATCH | Entier | JSON invalide ou malformé |
| JSON_ERROR_CTRL_CHAR | Entier | Erreur de caractère de contrôle |
| JSON_ERROR_SYNTAX | Entier | Erreur de syntaxe |
| JSON_ERROR_UTF8 | Entier | Caractères UTF-8 malformés. PHP 5.3 |
| JSON_ERROR_RECURSION | Entier | Valeurs de référence récursive invalides. PHP 5.5 |
| JSON_ERROR_INF_OR_NAN | Entier | Valeurs NAN ou INF invalides. PHP 5.5 |
| JSON_ERROR_UNSUPPORTED_TYPE | Entier | Type invalide. PHP 5.5 |
| JSON_ERROR_INVALID_PROPERTY_NAME | Entier | Nom de propriété invalide. PHP 7.0 |
| JSON_ERROR_UTF16 | Entier | Caractères UTF-16 malformés. PHP 7.0 |
| JSON_BIGINT_AS_STRING | Entier | |
| JSON_OBJECT_AS_ARRAY | Entier | |
| JSON_HEX_TAG | Entier | |
| JSON_HEX_AMP | Entier | |
| JSON_HEX_APOS | Entier | |
| JSON_HEX_QUOT | Entier | |
| JSON_FORCE_OBJECT | Entier | |
| JSON_NUMERIC_CHECK | Entier | |
| JSON_PRETTY_PRINT | Entier | |
| JSON_UNESCAPED_SLASHES | Entier | |
| JSON_PARTIAL_OUTPUT_ON_ERROR | Entier | |
| JSON_PRESERVE_ZERO_FRACTION | Entier | |
| JSON_UNESCAPED_LINE_TERMINATORS | Entier | |
| JSON_INVALID_UTF8_IGNORE | Entier | |
| JSON_INVALID_UTF8_SUBSTITUTE | Entier | |
| JSON_THROWN_ON_ERROR | Entier |