我在json响应中遇到问题,它给出的错误没有以正确的格式显示json。它显示低于错误
错误:第2行的解析错误:{“Shirts”[{“SubCategory”: ----------- ^期待'EOF','}',':',',',']',得到'['“
我的json回复是。
{
"Shirts" [{
"SubCategory": "Coalesce: Functioning On Impatience T-Shirt"
} {
"SubCategory": "Ink Eater: Krylon Bombear Destroyed Tee"
} {
"SubCategory": "The Only Children: Paisley T-Shirt"
} {
"SubCategory": "Zolof The Rock And Roll Destroyer: LOL Cat T-shirt"
} {
"SubCategory": "Coalesce: Functioning On Impatience T-Shirt"
} {
"SubCategory": "Coalesce: Functioning On Impatience T-Shirt"
} {
"SubCategory": "Ink Eater: Krylon Bombear Destroyed Tee"
} {
"SubCategory": "Ink Eater: Krylon Bombear Destroyed Tee"
} {
"SubCategory": "The Only Children: Paisley T-Shirt"
} {
"SubCategory": "The Only Children: Paisley T-Shirt"
} {
"SubCategory": "Zolof The Rock And Roll Destroyer: LOL Cat T-shirt"
} {
"SubCategory": "Zolof The Rock And Roll Destroyer: LOL Cat T-shirt"
} {
"SubCategory": "Zolof The Rock And Roll Destroyer: LOL Cat T-shirt"
} {
"SubCategory": "Zolof The Rock And Roll Destroyer: LOL Cat T-shirt"
} {
"SubCategory": "Zolof The Rock And Roll Destroyer: LOL Cat T-shirt"
} {
"SubCategory": "Coalesce: Functioning On Impatience T-Shirt"
} {
"SubCategory": "Ink Eater: Krylon Bombear Destroyed Tee"
} {
"SubCategory": "The Only Children: Paisley T-Shirt"
} {
"SubCategory": "Zolof The Rock And Roll Destroyer: LOL Cat T-shirt"
}], {
"Shoes" [{
"SubCategory": "ASICS\u00ae Men's GEL-Kayano\u00ae XII"
} {
"SubCategory": "Kenneth Cole New York Men's Con-verge Slip-on"
} {
"SubCategory": "CN Clogs Beach\/Garden Clog"
} {
"SubCategory": "CN Clogs Beach\/Garden Clog"
} {
"SubCategory": "CN Clogs Beach\/Garden Clog"
} {
"SubCategory": "CN Clogs Beach\/Garden Clog"
} {
"SubCategory": "CN Clogs Beach\/Garden Clog"
} {
"SubCategory": "CN Clogs Beach\/Garden Clog"
} {
"SubCategory": "CN Clogs Beach\/Garden Clog"
} {
"SubCategory": "CN Clogs Beach\/Garden Clog"
} {
"SubCategory": "CN Clogs Beach\/Garden Clog"
} {
"SubCategory": "ASICS\u00ae Men's GEL-Kayano\u00ae XII"
} {
"SubCategory": "ASICS\u00ae Men's GEL-Kayano\u00ae XII"
} {
"SubCategory": "ASICS\u00ae Men's GEL-Kayano\u00ae XII"
} {
"SubCategory": "ASICS\u00ae Men's GEL-Kayano\u00ae XII"
} {
"SubCategory": "Kenneth Cole New York Men's Con-verge Slip-on"
} {
"SubCategory": "Kenneth Cole New York Men's Con-verge Slip-on"
} {
"SubCategory": "Kenneth Cole New York Men's Con-verge Slip-on"
} {
"SubCategory": "Kenneth Cole New York Men's Con-verge Slip-on"
} {
"SubCategory": "Steven by Steve Madden Pryme Pump"
} {
"SubCategory": "Nine West Women's Lucero Pump"
} {
"SubCategory": "ECCO Womens Golf Flexor Golf Shoe"
} {
"SubCategory": "Anashria Womens Premier Leather Sandal"
} {
"SubCategory": "Anashria Womens Premier Leather Sandal"
} {
"SubCategory": "Anashria Womens Premier Leather Sandal"
} {
"SubCategory": "Anashria Womens Premier Leather Sandal"
} {
"SubCategory": "Anashria Womens Premier Leather Sandal"
} {
"SubCategory": "Anashria Womens Premier Leather Sandal"
} {
"SubCategory": "Anashria Womens Premier Leather Sandal"
} {
"SubCategory": "Steven by Steve Madden Pryme Pump"
} {
"SubCategory": "Steven by Steve Madden Pryme Pump"
} {
"SubCategory": "Steven by Steve Madden Pryme Pump"
} {
"SubCategory": "Steven by Steve Madden Pryme Pump"
} {
"SubCategory": "Nine West Women's Lucero Pump"
} {
"SubCategory": "Nine West Women's Lucero Pump"
} {
"SubCategory": "Nine West Women's Lucero Pump"
} {
"SubCategory": "ECCO Womens Golf Flexor Golf Shoe"
} {
"SubCategory": "ECCO Womens Golf Flexor Golf Shoe"
} {
"SubCategory": "ECCO Womens Golf Flexor Golf Shoe"
} {
"SubCategory": "CN Clogs Beach\/Garden Clog"
} {
"SubCategory": "ASICS\u00ae Men's GEL-Kayano\u00ae XII"
} {
"SubCategory": "Kenneth Cole New York Men's Con-verge Slip-on"
} {
"SubCategory": "Anashria Womens Premier Leather Sandal"
} {
"SubCategory": "Steven by Steve Madden Pryme Pump"
} {
"SubCategory": "Nine West Women's Lucero Pump"
} {
"SubCategory": "ECCO Womens Golf Flexor Golf Shoe"
}],
{
"Cell Phones" [{
"SubCategory": "Nokia 2610 Phone"
} {
"SubCategory": "BlackBerry 8100 Pearl"
} {
"SubCategory": "Sony Ericsson W810i"
} {
"SubCategory": "AT&T 8525 PDA"
} {
"SubCategory": "Samsung MM-A900M Ace"
} {
"SubCategory": "HTC Touch Diamond"
}],
{
"Cameras" [{
"SubCategory": "Canon Digital Rebel XT 8MP Digital SLR Camera"
} {
"SubCategory": " Argus QC-2185 Quick Click 5MP Digital Camera"
} {
"SubCategory": "Canon PowerShot A630 8MP Digital Camera with 4x Optical Zoom"
} {
"SubCategory": "Kodak EasyShare C530 5MP Digital Camera"
} {
"SubCategory": "SLR Camera Tripod"
} {
"SubCategory": "Universal Camera Case"
} {
"SubCategory": "Universal Camera Charger"
} {
"SubCategory": " Olympus Stylus 750 7.1MP Digital Camera"
}],
{
"Computers" [{
"SubCategory": "Apevia Black X-Cruiser Case ATX Mid-Tower Case (Default)"
} {
"SubCategory": "NZXT Lexa Silver Aluminum ATX Mid-Tower Case (Default)"
} {
"SubCategory": "AMD Phenom X4 9850 Black Ed. 2.50GHz Retail"
} {
"SubCategory": "22\" Syncmaster LCD Monitor"
} {
"SubCategory": "AMD A64 X2 3800+ 2.0GHz OEM"
} {
"SubCategory": "Western Digital - 1TB HD - 7200RPM"
} {
"SubCategory": "Western Digital 500GB HD - 7200RPM"
} {
"SubCategory": "Intel C2D E8400 3.0GHz Retail"
} {
"SubCategory": "24\" Widescreen Flat-Panel LCD Monitor"
} {
"SubCategory": "Intel Core 2 Extreme QX9775 3.20GHz Retail"
} {
"SubCategory": "Seagate 500GB HD - 5400RPM"
} {
"SubCategory": "Seagate 250GB HD - 5400RPM"
} {
"SubCategory": "19\" Widescreen Flat-Panel LCD Monitor"
} {
"SubCategory": "30\" Flat-Panel TFT-LCD Cinema HD Monitor"
} {
"SubCategory": "Sony VAIO 11.1\" Notebook PC"
} {
"SubCategory": "Microsoft Natural Ergonomic Keyboard 4000"
} {
"SubCategory": "Logitech Cordless Optical Trackman"
} {
"SubCategory": "Logitech diNovo Edge Keyboard"
} {
"SubCategory": "Microsoft Wireless Optical Mouse 5000"
} {
"SubCategory": "Gaming Computer"
} {
"SubCategory": "My Computer"
} {
"SubCategory": "Apple MacBook Pro MA464LL\/A 15.4\" Notebook PC"
} {
"SubCategory": "Acer Ferrari 3200 Notebook Computer PC"
} {
"SubCategory": "Crucial 2GB PC4200 DDR2 533MHz Memory"
} {
"SubCategory": "Crucial 1GB PC4200 DDR2 533MHz Memory"
} {
"SubCategory": "Crucial 512MB PC4200 DDR2 533MHz Memory"
} {
"SubCategory": "Computer"
} {
"SubCategory": "Toshiba M285-E 14\""
}],
{
"Hoodies" [{
"SubCategory": "The Get Up Kids: Band Camp Pullover Hoodie"
}],
{
"Living Room" [{
"SubCategory": "Ottoman"
} {
"SubCategory": "Chair"
} {
"SubCategory": "Couch"
} {
"SubCategory": "Magento Red Furniture Set"
}],
{
"Bedroom" [{
"SubCategory": "Akio Dresser"
} {
"SubCategory": "Barcelona Bamboo Platform Bed"
}],
}
我的代码是。
$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);
if($cat->getLevel()==3 && $cat->getIsActive()==1)
{
$category1 = Mage::getModel('catalog/category')->load($cat->getId());
$products = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect('name')
->addCategoryFilter($category1);
// echo "<b>".$cat->getName()."</b><br>";
$response["data"] = array();
$response["data"]["mainCategory"] = $cat->getName();
//echo json_encode($response);
echo "{";
echo json_encode($cat->getName());
echo "[";
foreach ($products as $product) { //print_r($product->getData());exit;
// echo " " . $product->getName() . "<br/>";
// echo json_encode($response);
$response["SubCategory"] = array();
$result["SubCategory"] = $product->getName();
echo json_encode( $result );
}
echo ']'.',';
}
}
}
echo '}';
答案 0 :(得分:1)
您应该在数组中的每个对象之间使用逗号。 所以不是:
{
"Shirts" [{
"SubCategory": "Coalesce: Functioning On Impatience T-Shirt"
} {
"SubCategory": "Ink Eater: Krylon Bombear Destroyed Tee"
}
你会:
{
"Shirts" [{
"SubCategory": "Coalesce: Functioning On Impatience T-Shirt"
}, {
"SubCategory": "Ink Eater: Krylon Bombear Destroyed Tee"
}, .......
答案 1 :(得分:1)
不要自己构建Json-String,很容易弄脏你的手。
为什么不使用array
来存储JsonString所代表的所有数据,然后输出它们以获取最终的JsonString( view 数据)
$data = array('Shirts' => array(), 'Shoes' => array())...;
echo json_encode($data);
答案 2 :(得分:0)
在最后一个元素后面有一个额外的逗号,这是一个语法错误。