As far as I understand, Magento has a JSON encode function by using zend framework. And my question, is there an option to force that function always return JSON object, not JSON array?
Mage::helper('core')->jsonEncode($array);
In native PHP (version 5.3.0 or above), I can achieve it by passing this constant "JSON_FORCE_OBJECT" into the second parameter of json_encode() function. Any help would be greatly appreciated.
Best Answer
jsonEncode ends up using
Zend_Json_Encoder
here you will find a function_encodeValue
which will either return an array or a object depending on the type of value.So if there is a object passed in then there will be an object returned. This really helps with multiple levels. For example.
Note that there are some special cases when encoding objects. As explained on the zend docs