Question: How to render JSON response?
In controller use, JSONModel
public function indexAction() {
$result = new JsonModel(array('data'=>array(
'key1' => 'value1',
'key2' => 'value2',
'key3' => 'value3',
'key4' => 'value4',
'key5' => 'value5'
)));
return $result;
}
In View file, use following the encode the array.
echo Zend\Json\Json::encode($this->jsonArray, Zend\Json\Json::TYPE_ARRAY);
Question: How to JSON Encode an php object in recursive way?
$arrayData = Zend\Json\Json::encode($phpObject, true);
Question: How to convert XML to JSON?
$jsonContents = Zend\Json\Json::fromXml($xmlStringContents, true);
Question: How to get PHP Array from JSON Encoded string in view file?
$encodedValue='[1,2,3,4,5,6]'; $arrayData = Zend\Json\Json::decode($encodedValue,Zend\Json\Json::TYPE_ARRAY); print_r($arrayData );die;
Question: How to get PHP Object from JSON Encoded string in view file?
$encodedValue='[1,2,3,4,5,6]'; $arrayData = Zend\Json\Json::decode($encodedValue,Zend\Json\Json::TYPE_OBJECT); print_r($arrayData );die;
Question: How to display JSON Encoded string in Pretty way?
$encodedValue='[1,2,3,4,5,6]';
$arrayData = Zend\Json\Json::prettyPrint($encodedValue, array("indent" => " "));
print_r($arrayData );die;
Question: How to check Ajax Request OR Not?
if($this->getRequest()->isXmlHttpRequest())){
/**
This is Ajax Request
**/
}else{
/**
This is NOT Ajax Request
**/
}
