⚡ Curated dev resources & dealsSponsored
DevToolBayFree Developer Tools

最常见的 JSON 报错与修法

尾逗号、单引号键、NaN……每种错误的成因与十秒修复法。

发布于·约 2 分钟阅读

Two JSON parsers meeting a broken document always agree on one thing: stop at the first bad byte. Knowing the usual suspects turns an opaque error into a 10-second fix.

1. Trailing commas

JavaScript object literals tolerate [1,2,3,]; JSON forbids it. Remove the comma before a closing bracket — the number one error, and the first thing to check when a config file works locally but fails in deploy.

2. Unquoted or single-quoted keys

JSON requires {"name": 1} — keys are strings with double quotes. Single quotes and bare keys come from pasting JavaScript or Python literals. Convert the source with the JSON Formatter after fixing quotes.

3-5. Quote and escape issues

  • Smart quotes: copy-paste from editors or chat apps swaps " for curly quotes that break parsing instantly.
  • Unescaped inner quotes: nested double quotes need \" escaping.
  • Raw control characters: real line breaks inside strings are illegal — use \n.

6-7. Comments and NaN

JSON has no comments — // or /* */ make the document invalid by spec, even when the language you copied from allows them. Likewise NaN, Infinity and undefined are not JSON values.

8-9. Structural slips

  • Missing colon or comma between members — often after edits.
  • Mismatched brackets, which long files make easy; the JSON Validator reports the exact line and column with an excerpt.

The 10-second workflow

Paste the document into the validator, fix the reported position, reformat with the formatter to see structure clearly, and only then minify with the minifier if the file ships to production. That order catches the remaining subtle errors too.

相关指南

文中提到的工具