Programmer's Reference Guide

導入

基本的な使用法

Zend_Json を使用するために、 静的な publicメソッドが 2 つ公開されています。 名前は Zend_Json::encode() および Zend_Json::decode() となります。

  1. // 値を取得します
  2. $phpNative = Zend_Json::decode($encodedValue);
  3.  
  4. // クライアントに返すために、それをエンコードします
  5. $json = Zend_Json::encode($phpNative);

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. // クライアントに返すために、それをエンコードします
  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.


導入

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