清单不是有效的 JSON。行:1,列:1,意外标记

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33758001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:09:34  来源:igfitidea点击:

Manifest is not valid JSON. Line: 1, column: 1, Unexpected token

jsongoogle-chrome-extension

提问by Joseph Young

keep getting this error: "Manifest is not valid JSON. Line: 1, column: 1, Unexpected token." i don't understand what the issue is with my code? here is what i have so far:

不断收到此错误:“清单不是有效的 JSON。行:1,列:1,意外标记。” 我不明白我的代码有什么问题?这是我到目前为止所拥有的:

{
    "manifest_version": 2,
    "name": "extension",
    "version": "1.0",
    "description": "My first Chrome extension.",
    "browser_action": {
        "default_icon": "icon.jpg",
        "popup": "popup.html"
    }
}

回答by Snoophogg

I had my manifest.json File Properties set with Build Action: None, in Visual Studio 2010.

我在 Visual Studio 2010 中使用 Build Action: None 设置了 manifest.json 文件属性。

Changing, in Visual Studio, to Content ensured the file was transferred when I deployed.

在 Visual Studio 中更改为 Content 可确保在我部署时传输文件。

enter image description here

在此处输入图片说明

Maybe yours is a similar issue.

也许你的也是类似的问题。

Edit

编辑

Given my downvote I thought I should expand and say that my point was that, given that at line 1, column 1 there is valid json, it is more likely that you are getting a 404 http response than the actual json file returned.

鉴于我的反对票,我认为我应该扩展并说我的观点是,鉴于在第 1 行第 1 列有有效的 json,与返回的实际 json 文件相比,您收到 404 http 响应的可能性更大。

So maybe the file isn't in the correct place or the server doesn't have permissions or whatever but the above is what had gone wrong with mine. Admittedly it is maybe too specific to what had gone wrong with mine and not definitely what was wrong with yours.

所以也许文件不在正确的位置,或者服务器没有权限或其他什么,但以上是我的问题。不可否认,它可能过于具体地说明了我的问题,而不是你的问题。

But the general point still stands, it's more likely caused by web server returning an http response for the json file, check the response in the network log in your browser.

但一般的观点仍然成立,这更有可能是由Web服务器为json文件返回http响应引起的,请在浏览器中检查网络日志中的响应。

回答by Datsos

Content-type

内容类型

Check the Content-Type of manifest.json in the Network tab. This needs to be application/jsoninstead of text/html.

在 Network 选项卡中检查 manifest.json 的 Content-Type。这需要是application/json而不是text/html

If you have wrong content-type, you may need to configure the settings of your webserver to correctly serve jsonfiles.

如果您的内容类型有误,您可能需要配置您的网络服务器的设置以正确提供json文件。

In my case I had to add it to the nginx sites-enabledfile:

就我而言,我必须将它添加到启用nginx站点的文件中:

location ~* .(jpg|jpeg|gif|png|css|js|ico|xml|svg|json)$

位置 ~* .(jpg|jpeg|gif|png|css|js|ico|xml|svg| json)$

回答by yogesh kumar

It seems your are using wrong values for browser_actionkey.To specify popup template you must use default_popup. It should be :

您似乎对browser_action键使用了错误的值。要指定弹出模板,您必须使用default_popup。它应该是 :

{
  "manifest_version": 2,
  "name": "extension",
  "version": "1.0",
  "description": "My first Chrome extension.",
  "browser_action": {
      "default_icon": "icon.jpg",
      "default_popup": "popup.html"
  }
}

回答by Alfred Wallace

The browser cannot find the manifest because the extension (MIME type). In Chrome DevTools > Network > Headers > Content-Type, the content-type of the manifest must match the actual manifest file extension (.jsonor .webmanifest). To resolve the 404 error, you need to declare the file type in web.config:

浏览器找不到清单,因为扩展名(MIME 类型)。在 Chrome DevTools > Network > Headers > Content-Type 中,清单的内容类型必须与实际清单文件扩展名(.json.webmanifest)匹配。要解决 404 错误,您需要在 中声明文件类型web.config

<staticContent>
  <mimeMap fileExtension=".json" mimeType="application/json" />
  <mimeMap fileExtension=".webmanifest" mimeType="application/manifest+json" />
</staticContent>

回答by ANIL PATEL

I added crossorigin attribute its worked.

我添加了 crossorigin 属性,它起作用了。

<link rel="manifest" crossorigin="use-credentials" href="%PUBLIC_URL%/manifest.json" />

回答by Volomike

In my particular case, I had this added in my manifest.json:

在我的特殊情况下,我在我的manifest.json

"storage": {
   "managed_schema": "storage.json"
}

But my storage.jsonwas empty. I had to edit my storage.jsonfile with this content:

但我的storage.json是空的。我必须storage.json用以下内容编辑我的文件:

{
   "$schema": "http://json-schema.org/draft-03/schema#",
   "type": "object",
   "properties": {
      "adminSettings": {
         "title": "A valid JSON string compliant with backup format.",
         "description": "All entries present will overwrite local settings.",
         "type": "string"
      }
   }
}

But in general, here's my advice when you get this Line 1 Col 1 Unexpected Token misleading error that basically translates to: Something is wrong in one or more sections of your JSON and may or may not have anything to do with syntax in this file.

但总的来说,当您收到此 Line 1 Col 1 Unexpected Token 误导性错误时,这是​​我的建议,该错误基本上转化为:JSON 的一个或多个部分出现问题,可能与此文件中的语法有关,也可能无关

  1. Move the most common, simple string things to the top of your manifest.jsonfile such as manifest_version, name, description, etc.
  2. Put the more elaborate array things towards the bottom, starting with the one you most suspect might be the problem.
  3. Save the file as UTF-8.
  4. Then, run it through an online validator.
  5. From there, if you still get the problem, then use another notepad and cut/paste items from the bottom of your JSON into another text file and try to upload your JSON again. (Note, you'll likely get an error of ending your JSON block with a comma with no following item. So, don't forget to undo that when you get an error.)
  6. Anyway, repeat step 4 until finally that Line 1 Col 1 error goes away. At that point, you know the offending section that you need to deal with.
  7. Paste everything else back except that section back into your manifest.json. This is how I found out that my storageblock was the problem. I looked at another Chrome Extension to see how they did things and realized my error.
  1. 将最常见的,简单的字符串递到你的顶部manifest.json文件,如manifest_versionnamedescription,等。
  2. 把更复杂的数组放在底部,从你最怀疑可能是问题的那个开始。
  3. 将文件另存为 UTF-8。
  4. 然后,通过在线验证器运行它。
  5. 从那里,如果您仍然遇到问题,请使用另一个记事本并将 JSON 底部的项目剪切/粘贴到另一个文本文件中,然后再次尝试上传您的 JSON。(请注意,您可能会收到一个错误,用逗号结束您的 JSON 块并且没有以下项目。因此,当您收到错误时不要忘记撤消该操作。)
  6. 无论如何,重复第 4 步,直到最后 Line 1 Col 1 错误消失。那时,您知道需要处理的违规部分。
  7. 将除该部分之外的所有其他内容粘贴回您的manifest.json. 这就是我发现我的storage块是问题的方式。我查看了另一个 Chrome 扩展程序,看看他们是如何做事并意识到我的错误的。

回答by mikeym

I know this is an old question but just in case the accepted answer above doesn't work for someone be sure to check the spelling of and path to the manifest.jsonare correct. I previously got the same error from a simple typo. Simple mistake but it happens!

我知道这是一个老问题,但以防万一上面接受的答案对某人不起作用,请务必检查拼写和路径manifest.json是否正确。我以前从一个简单的错字中得到了同样的错误。简单的错误,但它发生了!

回答by Honzik

This error also occurs when manifest is empty, or there is no manifest at all. If u use some framework, eg. Angular, just check whether you have manifest in your production build. If not - add "manifest.json" in your angular.json/angular-cli.json to assets array.

当清单为空或根本没有清单时,也会发生此错误。如果你使用一些框架,例如。Angular,只需检查您的生产版本中是否有清单。如果没有 - 在您的 angular.json/angular-cli.json 中将“manifest.json”添加到资产数组。

Example: "assets": ["assets", "favicon.ico", "manifest.json"]

例子: "assets": ["assets", "favicon.ico", "manifest.json"]

回答by Cesar Bielich

This is old but I had the same issue and found out I was not using the standard double quotes "in my script which I copied and pasted from the official Google Doc

这是旧的,但我遇到了同样的问题,并发现我没有"在我的脚本中使用标准的双引号,我从官方的 Google 文档中复制并粘贴了它

回答by Tony

Please try to save the manifest.json file as UTF8 format.

请尝试将 manifest.json 文件保存为 UTF8 格式。