向WordPress block.json添加attributes时遇JSON语法错误求助
问题排查与修复方案
错误根源
你碰到的JSON解析错误,是因为添加的attributes里有两个多余的尾逗号:
textContent配置块末尾的逗号(},)——这是attributes里最后一个属性,不需要加逗号attributes对象闭合前的逗号(},)——同样属于多余的尾逗号
JSON语法严格禁止对象最后一个键值对后面加逗号,这会直接导致解析器报错。
修复后的完整block.json代码
{ "$schema": "https://schemas.wp.org/trunk/block.json", "apiVersion": 2, "name": "space-rocket-blocks/hero", "version": "0.1.0", "title": "Hero", "category": "space-rocket-blocks", "icon": "smiley", "description": "For when you need a hero", "supports": { "html": false }, "attributes": { "headingContent": { "type": "string", "source": "text", "selector": "h1", "default": "How to build custom React Gutenberg Blocks" }, "textContent": { "type": "string", "source": "text", "selector": "p", "default": "Anim aute id magna aliqua ad ad non deserunt sunt. Qui irure qui lorem cupidatat commodo. Elit sunt amet fugiat veniam occaecat fugiat aliqua." } }, "textdomain": "hero", "editorScript": "file:./index.js", "editorStyle": "file:./index.css", "style": "file:./style-index.css" }
小提示
- 写JSON的时候别手滑加尾逗号,很多编辑器(比如VS Code)会实时提示这类语法错误
- 要是不确定,直接把代码贴到编辑器的JSON校验里,一眼就能找到问题
内容的提问来源于stack exchange,提问作者user17844877
相关产品推荐
相关产品推荐

