Zend_Json-Basics.xml 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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>,
  21. since it has no spacing or indentation. In order to make it easier, <classname>Zend_Json</classname>
  22. allows you to pretty-print <acronym>JSON</acronym> data in the human-readable format
  23. with <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 option array.
  34. Option <code>indent</code> allows to set indentation string - by default it's a single tab character.
  35. </para>
  36. </sect2>
  37. </sect1>
  38. <!--
  39. vim:se ts=4 sw=4 et:
  40. -->