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

调用music.json()触发‘dict’ object is not callable错误求解决

问题

在使用NoneBot框架编写机器人命令时,添加代码行music_json = music.json()触发错误:TypeError: 'dict' object is not callable。即使删除最后两行代码,错误依然存在,仅移除该行时程序正常运行。

代码片段

test = on_command("test", aliases={'/test',})
@test.handle()
async def test(bot: Bot, event: Event):
    with app.app_context(): 
        music, status = api.songs.songs_song_get(1145141919810, 'alea')
    music_json = music.json()
    msg = f'{music_json}'
    await bot.send(event, msg)

报错堆栈

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Users\Administrator\Desktop\lib\multiprocessing\spawn.py", line 116, in spawn_main
    exitcode = _main(fd, parent_sentinel)
  File "C:\Users\Administrator\Desktop\lib\multiprocessing\spawn.py", line 129, in _main
    return self._bootstrap(parent_sentinel)
  File "C:\Users\Administrator\Desktop\lib\multiprocessing\process.py", line 315, in _bootstrap
    self.run()
  File "C:\Users\Administrator\Desktop\lib\multiprocessing\process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\Administrator\Desktop\lib\site-packages\uvicorn\_subprocess.py", line 76, in subprocess_started
    target(sockets=sockets)
  File "C:\Users\Administrator\Desktop\lib\site-packages\uvicorn\server.py", line 60, in run
    return asyncio.run(self.serve(sockets=sockets))
  File "C:\Users\Administrator\Desktop\lib\asyncio\runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "C:\Users\Administrator\Desktop\lib\asyncio\base_events.py", line 629, in run_until_complete
    self.run_forever()
  File "C:\Users\Administrator\Desktop\lib\asyncio\base_events.py", line 596, in run_forever
    self._run_once()
  File "C:\Users\Administrator\Desktop\lib\asyncio\base_events.py", line 1890, in _run_once
    handle._run()
  File "C:\Users\Administrator\Desktop\lib\asyncio\events.py", line 80, in _run
    self._context.run(self._callback, *self._args)
  File "C:\Users\Administrator\Desktop\lib\site-packages\nonebot\message.py", line 142, in _check_matcher
    await _run_matcher(Matcher, bot, event, state, stack, dependency_cache)
> File "C:\Users\Administrator\Desktop\lib\site-packages\nonebot\message.py", line 186, in _run_matcher
    await matcher.run(bot, event, state, stack, dependency_cache)
  File "C:\Users\Administrator\Desktop\lib\site-packages\nonebot\internal\matcher.py", line 716, in run
    await self.simple_run(bot, event, state, stack, dependency_cache)
  File "C:\Users\Administrator\Desktop\lib\site-packages\nonebot\internal\matcher.py", line 688, in simple_run
    await handler(
  File "C:\Users\Administrator\Desktop\lib\site-packages\nonebot\dependencies\__init__.py", line 108, in __call__
    return await cast(Callable[..., Awaitable[R]], self.call)(**values)
  File "C:\Users\Administrator\Kuro-bot\.\kuro_bot\plugins\arcfur\__init__.py", line 39, in test
    music_json = music.json()
TypeError: 'dict' object is not callable

解决方案

  • 问题成因:api.songs.songs_song_get返回的music已经是Python字典类型,不是HTTP响应对象。你试图调用字典的json()方法,但字典本身没有这个可调用方法,因此触发错误。
  • 基础解决方法:直接使用music变量即可,无需调用json()。修改后代码:
test = on_command("test", aliases={'/test',})
@test.handle()
async def test(bot: Bot, event: Event):
    with app.app_context(): 
        music, status = api.songs.songs_song_get(1145141919810, 'alea')
    msg = f'{music}'
    await bot.send(event, msg)
  • 优化输出格式:如果需要更易读的字典展示,可以用json.dumps格式化:
import json

test = on_command("test", aliases={'/test',})
@test.handle()
async def test(bot: Bot, event: Event):
    with app.app_context(): 
        music, status = api.songs.songs_song_get(1145141919810, 'alea')
    music_str = json.dumps(music, indent=2, ensure_ascii=False)
    msg = f'{music_str}'
    await bot.send(event, msg)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 02:20:30