You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

手动向JSON文件添加多个字典可行吗?报错问题求助

手动编辑JSON添加字典的可行性及错误修正

当然可以手动向JSON文件添加字典(也就是数组内嵌对象),报错是因为你的代码不符合JSON语法规范,和手动编辑本身无关。

你的代码里的问题:

  • 对象键名必须用双引号:JSON要求所有对象的键(比如id、weight、item)必须用双引号包裹,你写的是无引号的形式,这是JavaScript对象的写法,JSON不认可。
  • 字符串只能用双引号:JSON里字符串必须用双引号,不能用单引号,比如你写的'Mango'要改成"Mango"。

修正后的正确代码:

{
    "Isle of Dreams": {
        "name": "Isle of Dreams",
        "description": "An island surrounded by mist and myth, here dreamers come to fulfill their deepest desires."
    },
    "the Temple of Shadows": {
        "name": "the Temple of Shadows",
        "description": "",
        "droptable": [
            { "id": 0, "weight": 0.2, "item": "Lemon" },
            { "id": 1, "weight": 0.3, "item": ["Grape", "Orange", "Apple"] },
            { "id": 2, "weight": 0.4, "item": "Mango" },
            { "id": 3, "weight": 0.1, "item": 3 }
        ]
    }
}

验证建议:

可以用在线JSON校验工具检查修正后的代码,确认格式合法后再保存到文件,就能正常被Node.js或其他解析工具读取了。

内容的提问来源于stack exchange,提问作者xKwm1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 15:37:17