Introduction

Utilisation de base

L'utilisation de Zend_Json implique l'emploi des deux méthodes statiques publiques disponibles : Zend_Json::encode() et Zend_Json::decode().

  1. // Obtention d'une valeur
  2. $phpNatif = Zend_Json::decode($valeurCodee);
  3.  
  4. // Codage pour renvoi au client :
  5. $json = Zend_Json::encode($phpNatif);

Pretty-printing JSON

Sometimes, it may be hard to explore JSON data generated by Zend_Json::encode(), since it has no spacing or indentation. In order to make it easier, Zend_Json allows you to pretty-print JSON data in the human-readable format with Zend_Json::prettyPrint().

  1. // Encode it to return to the client:
  2. $json = Zend_Json::encode($phpNative);
  3. if($debug) {
  4. echo Zend_Json::prettyPrint($json, array("indent" => " "));
  5. }

Second optional argument of Zend_Json::prettyPrint() is an option array. Option indent allows to set indentation string - by default it's a single tab character.


Introduction