Zend_Json-Basics.xml 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.json.basics">
  4. <title>Basic Usage</title>
  5. <para>
  6. Usage of <classname>Zend_Json</classname> involves using the two public static
  7. methods available: <methodname>Zend_Json::encode()</methodname> and
  8. <methodname>Zend_Json::decode()</methodname>.
  9. </para>
  10. <programlisting language="php"><![CDATA[
  11. // Retrieve a value:
  12. $phpNative = Zend_Json::decode($encodedValue);
  13. // Encode it to return to the client:
  14. $json = Zend_Json::encode($phpNative);
  15. ]]></programlisting>
  16. <sect2 id="zend.json.basics.prettyprint">
  17. <title>Pretty-printing JSON</title>
  18. <para>
  19. Sometimes, it may be hard to explore <acronym>JSON</acronym> data generated by
  20. <methodname>Zend_Json::encode()</methodname>, since it has no spacing or indentation.
  21. In order to make it easier, <classname>Zend_Json</classname> allows you to pretty-print
  22. <acronym>JSON</acronym> data in the human-readable format with
  23. <methodname>Zend_Json::prettyPrint()</methodname>.
  24. </para>
  25. <programlisting language="php"><![CDATA[
  26. // Encode it to return to the client:
  27. $json = Zend_Json::encode($phpNative);
  28. if($debug) {
  29. echo Zend_Json::prettyPrint($json, array("indent" => " "));
  30. }
  31. ]]></programlisting>
  32. <para>
  33. Second optional argument of <methodname>Zend_Json::prettyPrint()</methodname> is an
  34. option array. Option <property>indent</property> allows to set indentation string - by
  35. default it's a single tab character.
  36. </para>
  37. </sect2>
  38. </sect1>
  39. <!--
  40. vim:se ts=4 sw=4 et:
  41. -->