JSON文件报缺少分号错误 VSCode无法定位问题行求助
错误根源是你将纯JSON结构直接写在了.js后缀的JavaScript文件中,JS引擎无法识别裸写的JSON对象语法:
JS解析器遇到行首的
{时,会优先把它判定为代码块的起始标记,而非对象字面量的开头,所以解析到第二行的:时不符合JS代码块的语法规则,才会抛出缺失分号的错误提示,并不是真的需要在对应位置补分号。
以下是两种可行的解决方案:
方案1:保留.js文件后缀,改为JS变量导出
给你的JSON结构添加变量声明和导出语句,修改后的完整代码如下:
const banCardData = { "data": [ { "id": 72053645, "name": "Weather Report", "type": "Flip Effect Monster", "desc": "FLIP: Destroys all of your opponent's face-up \"Swords of Revealing Light\" on the field. If \"Swords of Revealing Light\" is destroyed, you can perform your Battle Phase twice this turn (or your next turn, if activated during your opponent's turn).", "atk": 950, "def": 1500, "level": 4, "race": "Aqua", "attribute": "WATER", "banlist_info": { "ban_tcg": "Banned", "ban_ocg": "Limited" }, "card_images": [ { "id": 72053645, "image_url": "https://storage.googleapis.com/ygoprodeck.com/pics/72053645.jpg", "image_url_small": "https://storage.googleapis.com/ygoprodeck.com/pics_small/72053645.jpg" } ] }, { "id": 81122844, "name": "Wind-Up Carrier Zenmaity", "type": "XYZ Monster", "desc": "2 Level 3 monsters\nOnce per turn: You can detach 1 Xyz Material from this card; Special Summon 1 \"Wind-Up\" monster from your hand or Deck. When a face-up \"Wind-Up\" monster on the field is destroyed and sent to your Graveyard (except during the Damage Step): You can detach 1 Xyz Material from this card to target that monster; return that target to the hand.", "atk": 1500, "def": 1500, "level": 3, "race": "Machine", "attribute": "WATER", "archetype": "Wind-Up", "banlist_info": { "ban_tcg": "Banned", "ban_ocg": "Limited" }, "card_images": [ { "id": 81122844, "image_url": "https://storage.googleapis.com/ygoprodeck.com/pics/81122844.jpg", "image_url_small": "https://storage.googleapis.com/ygoprodeck.com/pics_small/81122844.jpg" } ] } ] }; export default banCardData;
其他文件导入时直接用import banCardData from '@/services/testbanlist.js'即可正常读取数据。
方案2:改为纯JSON文件存储
将testbanlist.js重命名为testbanlist.json,删掉文件末尾多余的分号,确保文件内容是纯JSON结构:
{ "data": [ { "id": 72053645, "name": "Weather Report", "type": "Flip Effect Monster", "desc": "FLIP: Destroys all of your opponent's face-up \"Swords of Revealing Light\" on the field. If \"Swords of Revealing Light\" is destroyed, you can perform your Battle Phase twice this turn (or your next turn, if activated during your opponent's turn).", "atk": 950, "def": 1500, "level": 4, "race": "Aqua", "attribute": "WATER", "banlist_info": { "ban_tcg": "Banned", "ban_ocg": "Limited" }, "card_images": [ { "id": 72053645, "image_url": "https://storage.googleapis.com/ygoprodeck.com/pics/72053645.jpg", "image_url_small": "https://storage.googleapis.com/ygoprodeck.com/pics_small/72053645.jpg" } ] }, { "id": 81122844, "name": "Wind-Up Carrier Zenmaity", "type": "XYZ Monster", "desc": "2 Level 3 monsters\nOnce per turn: You can detach 1 Xyz Material from this card; Special Summon 1 \"Wind-Up\" monster from your hand or Deck. When a face-up \"Wind-Up\" monster on the field is destroyed and sent to your Graveyard (except during the Damage Step): You can detach 1 Xyz Material from this card to target that monster; return that target to the hand.", "atk": 1500, "def": 1500, "level": 3, "race": "Machine", "attribute": "WATER", "archetype": "Wind-Up", "banlist_info": { "ban_tcg": "Banned", "ban_ocg": "Limited" }, "card_images": [ { "id": 81122844, "image_url": "https://storage.googleapis.com/ygoprodeck.com/pics/81122844.jpg", "image_url_small": "https://storage.googleapis.com/ygoprodeck.com/pics_small/81122844.jpg" } ] } ] }
目前主流前端脚手架都支持直接导入JSON文件,导入语句为import banCardData from '@/services/testbanlist.json'。
内容的提问来源于stack exchange,提问作者Patrick Finnigan
相关产品推荐
相关产品推荐

