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

JSON文件已按行分隔却无法加载,问题出在哪里?

问题

尝试加载一份示例JSON文件:

{
  "bot_token": "##############################",
  "GOOGLE_APPLICATION_CREDENTIALS": "assets/firebase-adminsdk.json",
  "databaseURL": "https://########-default-rtdb.firebaseio.com/",
  "storageBucket": "gs://##########.appspot.com",
  "owner_id": 0000000000,
  "admins": [0000000000]
}

使用以下Python代码加载:

# Load configuration file
with open('assets/config.json', 'r') as f:
    config = json.load(f)

出现如下错误:

Exception has occurred: JSONDecodeError
Expecting ',' delimiter: line 6 column 16 (char 251)
  File "D:\SingleStoreBot\bot_init.py", line 58, in <module>
    config = json.load(f)
             ^^^^^^^^^^^^
json.decoder.JSONDecodeError: Expecting ',' delimiter: line 6 column 16 (char 251)

已确认所有行都已分隔,请问问题出在哪里?

解答

问题出在JSON的数字格式不符合规范:JSON不允许数字带有前导零,0000000000这种写法属于非法格式,解析器无法识别,因此抛出了分隔符错误(实际是数字格式问题导致解析中断)。

你可以通过两种方式修复:

  • 将ID改为字符串类型,用双引号包裹,保留前导零:
    {
      "bot_token": "##############################",
      "GOOGLE_APPLICATION_CREDENTIALS": "assets/firebase-adminsdk.json",
      "databaseURL": "https://########-default-rtdb.firebaseio.com/",
      "storageBucket": "gs://##########.appspot.com",
      "owner_id": "0000000000",
      "admins": ["0000000000"]
    }
    
  • 如果不需要保留前导零,直接去掉多余的零,写成合法的数字:
    {
      "bot_token": "##############################",
      "GOOGLE_APPLICATION_CREDENTIALS": "assets/firebase-adminsdk.json",
      "databaseURL": "https://########-default-rtdb.firebaseio.com/",
      "storageBucket": "gs://##########.appspot.com",
      "owner_id": 0,
      "admins": [0]
    }
    

修改后再运行Python代码即可正常加载配置文件。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 13:43:25