电子商务网站中类别的JSON数据存储

时间:2017-12-18 17:16:11

标签: javascript json

我正在为电子商务网站创建一个示例json数据存储区。我无法确定代码中的问题。 JSON验证器给了我这个警告:

  

第3行的解析错误:
  ......类别“:”手机“;
        “清单”:
  ----------------------- ^
  期待'EOF','}',':',',',']',得到'未定义'。

[
  {
     "category": "mobiles";
      "lists":[
       {
         "companyName" : "samsung";
         "modelName" : "s8+";
         "warranty" : "2 years";
         "price" : 45000;
       }, 
       {
         "companyName" : "samsung";
         "modelName" : "s8+";
         "warranty" : "2 years";
         "price" : 45000;
       },
       {
         "companyName" : "samsung";
         "modelName" : "s8+";
         "warranty" : "2 years";
         "price" : 45000;
       }
      ]
  },
  {
  "category": "books",
     "lists":[
       {
         "bookTag" : "java";
         "bookName" : "the complete reference";
         "authorName" : "herbert shildit";
         "edition" : "2016";
         "price" : 450;
       }, 
       {
         "bookTag" : "dataStructure";
         "bookName" : "getting started with data structure";
         "authorName" : "adam kooper";
         "edition" : "2017";
         "price" : 500;
       },
       {
         "bookTag" : "javascript";
         "bookName" : "javascript :novice to ninja";
         "authorName" : "herbert shildit";
         "edition" : "2016";
         "price" : 350;
       }
      ]
  },
  {
  "category": "shoes";
     "lists":[
       {
         "companyName" : "nike";
         "color" : "white";
         "type" : "casual";
         "price" : 4500;
       }, 
       {
         "companyName" : "adidas";
         "color" : "brown";
         "type" : "sports";
         "price" : 4300;
       },
       {
         "companyName" : "sparks";
         "color" : "blue";
         "type" : "sports";
         "price" : 3000;
       }
      ]
  }
]

1 个答案:

答案 0 :(得分:1)

     {
         "companyName" : "samsung";
         "modelName" : "s8+";
         "warranty" : "2 years";
         "price" : 45000;
       }

这不是有效的JSON。请考虑使用,代替;

相反,你的JSON应该是:

  {
     "companyName" : "samsung",
     "modelName" : "s8+",
     "warranty" : "2 years",
     "price" : 45000
   }

另外,请注意最后一个条目最后没有任何分隔符。因此,您应该考虑为所有JSON执行此操作。