V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
PbCopy111
V2EX  ›  Python

Python 学习问题, json 格式不对

  •  
  •   PbCopy111 · 2019-10-14 13:57:52 +08:00 · 2696 次点击
    这是一个创建于 1627 天前的主题,其中的信息可能已经有所发展或是发生改变。

    学习 python,用的一个源代码:

    https://github.com/rollingstarky/Python-Voice-Assistant/blob/master/src/ai.py

    就是录音+百度识别+图灵

    这个代码有点老了,我这里运行的错误提示如下:

    please say something
    you said: 你好
    Traceback (most recent call last):
      File "aitalk.py", line 129, in <module>
        response = robot(request)
      File "aitalk.py", line 78, in robot
        response_dict = json.loads(response.text)
      File "/Users/v2ex/.pyenv/versions/3.6.0/lib/python3.6/json/__init__.py", line 354, in loads
        return _default_decoder.decode(s)
      File "/Users/v2ex/.pyenv/versions/3.6.0/lib/python3.6/json/decoder.py", line 339, in decode
        obj, end = self.raw_decode(s, idx=_w(s, 0).end())
      File "/Users/v2ex.pyenv/versions/3.6.0/lib/python3.6/json/decoder.py", line 357, in raw_decode
        raise JSONDecodeError("Expecting value", s, err.value) from None
    json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
    

    请教应该怎么修改啊??

    7 条回复    2019-10-17 13:02:02 +08:00
    guyeu
        1
    guyeu  
       2019-10-14 14:03:18 +08:00
    你把 response 打印出来不就能知道 json 哪里不对了。。
    ysc3839
        2
    ysc3839  
       2019-10-15 00:39:38 +08:00 via Android
    这个代码怎么这么奇怪? requests 有 .post() 的方法,还用 request('POST', ...)。requests 有 res.json() 的方法,还用 json.loads()。
    l4ever
        3
    l4ever  
       2019-10-15 08:47:01 +08:00
    @ysc3839 水平不行呗.
    请求 api 完全不判断一下能否返回结果啊.
    我 request 之后都会 try 一下.


    result = response_dict["results"][0]["values"]["text"]
    还有这个,万一 dict 没有 results 怎么办?
    PbCopy111
        4
    PbCopy111  
    OP
       2019-10-15 09:27:15 +08:00
    @rollingstarky 看看作者在不在这里。。。。。
    craiiz
        5
    craiiz  
       2019-10-15 16:13:24 +08:00
    一般情况是第 78 行的时候发生了错误,导致返回的内容已经不是 json,变成了 html 或其他的数据
    cominghome
        6
    cominghome  
       2019-10-17 09:33:02 +08:00
    我在想你的学习计划是不是安排得有问题,拧螺丝都没拧熟就准备开始造火箭啦?
    PbCopy111
        7
    PbCopy111  
    OP
       2019-10-17 13:02:02 +08:00
    @cominghome 没办法,都说看了基础,开始做程序,我就看看别人的源码,然后慢慢来呗。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3470 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 10:56 · PVG 18:56 · LAX 03:56 · JFK 06:56
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.