这是我的代码: -
try
{
$cur_category = array();
$category = Mage::getModel('catalog/category');
$tree = $category->getTreeModel();
$tree->load();
$ids = $tree->getCollection()->getAllIds();
if ($ids)
{
foreach ($ids as $id)
{
$cat = Mage::getModel('catalog/category');
$cat->load($id);
$node["category_id"] = $cat->getId();
$node["name"] = $cat->getName();
$node["image_url"] = $cat->getImageUrl();
$cur_category[] = $node;
}
}
echo json_encode($cur_category);
}catch (Exception $e){
$message = $e->getMessage();
echo json_encode(array("status" => "500", "error" => $message));
}
代码的输出是: -
[{"category_id":"1","name":"Root Catalog","image_url":false},{"category_id":"2","name":"Electronics","image_url":"http:\/\/localhost:81\/magento\/media\/catalog\/category\/Apple-MGX92HN-A-MacBook-Pro-SDL150017386-1-c0afa.jpg"},{"category_id":"3","name":"Laptops","image_url":false},{"category_id":"4","name":"Men","image_url":false},{"category_id":"5","name":"Women","image_url":false},{"category_id":"6","name":"Baby & Kids","image_url":false},{"category_id":"7","name":"Home & Furniture","image_url":false},{"category_id":"8","name":"Accessories","image_url":false},{"category_id":"9","name":"Formal Mens Jeans","image_url":false}]null
问题是最终的,因为你可以看到null即将发布如何删除任何建议。
答案 0 :(得分:0)
如果我理解正确,你在json对象里面的所有数据都在方括号[]里面?
所以,如果没有什么遗漏。我建议只从json_encode($cur_category)
解析你的返回字符串。为了快速查看,我认为您的$cur_category
最后确实有一个空的[]行,因此返回null
我的主要观点是使用解析结果字符串,并确保$cur_category
没有空行。
使用
捕捉阵列echo(print_f($cur_category,true));