Json表格数据不合适

时间:2016-04-20 12:20:37

标签: php json magento-1.9

我已经使用递归技术创建了用于获取类别和子类别的rest api。这是我的代码: -

public function getNodeChildrenData(Varien_Data_Tree_Node $node)
{
    $data = array(
        'title' => $node->getData('name'),
        'url'   => $node->getData('url_key'),

    );

    foreach ($node->getChildren() as $childNode) {
        if (!array_key_exists('children', $data)) {
            $data['children'] = array();
        }

        $data['children'][$childNode->getData('entity_id')] = $this->getNodeChildrenData($childNode);
    }
    return $data;
}

public function getCategoryTree($recursionLevel, $storeId = 1)
{
    $parent = Mage::app()->getStore()->getRootCategoryId();    
    $tree = Mage::getResourceModel('catalog/category_tree');
    /* @var $tree Mage_Catalog_Model_Resource_Category_Tree */

    $nodes = $tree->loadNode($parent)
        ->loadChildren($recursionLevel)
        ->getChildren();
    $tree->addCollectionData(null, false, $parent);

    $categoryTreeData = array();
    foreach ($nodes as $node) {
        $categoryTreeData[$node->getData('entity_id')] = $this->getNodeChildrenData($node);
    }

    return $categoryTreeData;
} 

public function _retrieveCollection()
{ 
    $cur_category = array();
    $cur_category[] = $this->getCategoryTree(2);   

    return json_encode($cur_category);    
}

我得到的结果是以下格式: -

"[{
    \"1\": {
        \"title\":\"Root Catalog\",\"url\":null,\"children\":{
            \"2\":{
                \"title\":\"Electronics\",\"url\":null, \"children\":{
                    \"3\":{\"title\":\"Laptops\",\"url\":\"laptops\"}
                 }
            },
            \"4\":{
                \"title\":\"Men\",\"url\":\"apparels\",\"children\":{
                    \"9\":{\"title\":\"Formal Mens Jeans\",\"url\":\"formal-mens-jeans\"}
                }
            },
            \"5\":{\"title\":\"Women\",\"url\":\"women\"},
            \"6\":{\"title\":\"Baby & Kids\",\"url\":\"baby-kids\"},
            \"7\":{\"title\":\"Home & Furniture\",\"url\":\"home-furniture\"}
        }
    }
}]" 
你可以告诉我哪里出错了吗?

0 个答案:

没有答案