JS脚本上的语法错误

Syntax error on JS script

本文关键字:语法 错误 脚本 JS      更新时间:2023-09-26

我有以下.js文件,Dreamweaver 在第 2 行报告了一个语法错误。

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "id": 0,
      "properties": {
        "Id": 0,
        "Title": "River & Trail HQ",
        "Other": null,
        "Parking": "Yes"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          -8649997.6690437607,
          4769179.73322534
        ]
      }
    }
  ]
}

似乎有什么问题

"type": "FeatureCollection",

线。

如果这是整个 JavaScript 文件,那么它会给你一个错误,因为它被解释为一个块而不是一个对象文字。要解决此问题,您可以将其分配给某些内容,例如

var someObj = {
    "type": "FeatureCollection",
    ...
}

您可能还会考虑一个 JSON 文件(可能是这种情况,因为自由浮动的对象文字无论如何都不会对您有多大好处)。JSON 和 JavaScript 是不一样的。如果您确实想要一个 JSON 文件,请将其保存为 ( .json )。

尝试

var x = {
"type": "FeatureCollection",
"features": [
{ 
    "type": "Feature", "id": 0, "properties": 
    { 
        "Id": 0, "Title": "River & Trail HQ", "Other": null, "Parking": "Yes" 
    }, 
    "geometry": 
    { 
        "type": "Point", "coordinates": [ -8649997.6690437607, 4769179.73322534 ] 
    } 
}
]};
http://jsonlint.com 将其

显示为有效的JSON,我在目视检查中看不到任何问题。