无法将Magento对象数组响应转换为json格式

时间:2015-06-19 04:56:24

标签: php mysql arrays json magento

我正在尝试将magento对象数组转换为json,但它会给出结果{}

$facetColor = Mage::getModel('facets/color')->getCollection();

打印数组给我

print "<pre>";
print_r($facetColor);

gamer_Facets_Model_Resource_Color_Collection Object
(
    [_joinedFields:protected] => Array
        (
        )

    [_model:protected] => facets/color
    [_resourceModel:protected] => facets/color
    [_resource:protected] => Gamer_Facets_Model_Resource_Color Object
        (
            [_resources:protected] => Mage_Core_Model_Resource Object
                (
                    [_connectionTypes:protected] => Array
                        (
                        )

                    [_connections:protected] => Array
                        (
                            [core_read] => Varien_Db_Adapter_Pdo_Mysql Object
                                (
                                    [_defaultStmtClass:protected] => Varien_Db_Statement_Pdo_Mysql
                                    [_transactionLevel:protected] => 0
                                    [_connectionFlagsSet:protected] => 1
                                    [_ddlCache:protected] => Array
                                        (
                                            [1] => Array
                                                (
                                                    [log_visitor_info] => Array
                                                        (
                                                            [visitor_id] => Array
                                                                (
                                                                    [SCHEMA_NAME] => 
                                                                    [TABLE_NAME] => log_visitor_info
                                                                    [COLUMN_NAME] => visitor_id
                                                                    [COLUMN_POSITION] => 1
                                                                    [DATA_TYPE] => bigint
                                                                    [DEFAULT] => 0
                                                                    [NULLABLE] => 
                                                                    [LENGTH] => 
                                                                    [SCALE] => 
                                                                    [PRECISION] => 
                                                                    [UNSIGNED] => 1
                                                                    [PRIMARY] => 1
                                                                    [PRIMARY_POSITION] => 1
                                                                    [IDENTITY] => 
                                                                )

                                                            [http_referer] => Array
                                                                (
                                                                    [SCHEMA_NAME] => 
                                                                    [TABLE_NAME] => log_visitor_info
                                                                    [COLUMN_NAME] => http_referer
                                                                    [COLUMN_POSITION] => 2
                                                                    [DATA_TYPE] => varchar
                                                                    [DEFAULT] => 
                                                                    [NULLABLE] => 1
                                                                    [LENGTH] => 255
                                                                    [SCALE] => 
                                                                    [PRECISION] => 
                                                                    [UNSIGNED] => 
                                                                    [PRIMARY] => 
                                                                    [PRIMARY_POSITION] => 
                                                                    [IDENTITY] => 
                                                                )

然后我使用此代码将其转换为json格式:

$jsonData = Mage::helper('core')->jsonEncode((array)$facetColor);
echo $jsonData;

并获得结果{}

我也尝试使用json_encode功能,但仍然没有运气,结果相同。

1 个答案:

答案 0 :(得分:2)

请尝试以下操作:

$jsonData = Mage::helper('core')->jsonEncode($facetColor->getData());

$jsonData = json_encode($facetColor->getData());

希望这有帮助!!