Programmer's Reference Guide

Einführung

Grundlegende Verwendung

Die Verwendung von Zend_Json bedingt den Gebrauch der beiden öffentlich verfügbaren, statischen Methoden Zend_Json::encode() und Zend_Json::decode().

  1. // Empfange einen Wert
  2. $phpNative = Zend_Json::decode($encodedValue);
  3.  
  4. // Kodiere ihn für die Rückgabe an den Client:
  5. $json = Zend_Json::encode($phpNative);

Schön-drucken von JSON

Manchmal ist es schwer JSON Daten zu durchsuchen welche von Zend_Json::encode() erzeugt wurden da Sie keine Leerzeichen oder Einrückungen enthalten. Um das einfacher zu machen erlaubt es Zend_Json JSON schön ausgedruckt, in einem menschlich-lesbaren Format, zu erhalten, indem man Zend_Json::prettyPrint() verwendet.

  1. // Kodieren und an den Client zurückzugeben:
  2. $json = Zend_Json::encode($phpNative);
  3. if($debug) {
  4.     echo Zend_Json::prettyPrint($json, array("indent" => " "));
  5. }

Das zweite optionale Argument von Zend_Json::prettyPrint() ist ein Optionen Array. Die Option indent erlaubt es einen String für die Einrückung zu definieren - standardmäßig ist das ein einzelnes Tabulator Zeichen.


Einführung

Comments

It's not documented here (or in the API docs) but if Zend_Json fails to decode your input it'll throw a Zend_Json_Exception. To find this information required wading through the source code.
> To find this information required wading through the source code.

That's the beauty of PHP. It is impossible to trace errors and in the end it really just wants to be like Java!
One way to get the encoded JSON data sent to your action:

$json = $this->getRequest()->getRawBody();
$data = Zend_Json::decode($json);

Now $data will be the JSON data in array format.

+ Add A Comment

Please do not report issues via comments; use the ZF Issue Tracker.

If you have a JIRA/Crowd account, we suggest you login first before commenting.

  • BBCode is allowed in the comment markup

  • Select a Version

    Languages Available

    Components

    Search the Manual