简体   繁体   中英

There is an error in json response

I have a problem in json response, its giving error not showing json in proper format. its showing below error

Error: Parse error on line 2: { "Shirts" [{ "SubCategory": -----------^ Expecting 'EOF', '}', ':', ',', ']', got '['"

My json response is.

{
    "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"
                                }],
                            }

and my code is.

$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 " &nbsp; &nbsp; &nbsp; " . $product->getName() .  "<br/>";
                       // echo json_encode($response);
                        $response["SubCategory"] = array();
                        $result["SubCategory"] = $product->getName();
                        echo json_encode( $result );
                }
                echo ']'.',';

             }
          }

        }
        echo '}';

You should have commas in between each of your objects in the array. So instead of it being:

{ "Shirts" [{ "SubCategory": "Coalesce: Functioning On Impatience T-Shirt" } { "SubCategory": "Ink Eater: Krylon Bombear Destroyed Tee" }

You would have:

{ "Shirts" [{ "SubCategory": "Coalesce: Functioning On Impatience T-Shirt" }, { "SubCategory": "Ink Eater: Krylon Bombear Destroyed Tee" }, .......

Don't build Json-String by yourself, it is easily to get your hands dirty.

Why don't use a array to store all the data which JsonString representing, and then output them to get the final JsonString( view of data)

$data = array('Shirts' => array(), 'Shoes' => array())...;

echo json_encode($data);

最后一个元素后面有一个逗号,这是语法错误。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM