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
wdc63
V2EX  ›  Python

Python json 解析问题,在线等

  •  
  •   wdc63 · 2021-05-12 11:18:47 +08:00 · 2022 次点击
    这是一个创建于 1051 天前的主题,其中的信息可能已经有所发展或是发生改变。

    当使用 python 的 json 解析这个页面时: http://polygons.openstreetmap.fr/get_geojson.py?id=4738519 出现 Runtime error (ValueErrorException): No JSON object could be decoded 报错

    12 条回复    2021-05-12 18:46:54 +08:00
    009694
        1
    009694  
       2021-05-12 11:32:23 +08:00 via Android
    首先没有展示任何代码 几乎很难有人能凭空想象来帮你 。 其次这是 geojson 不过并不影响 json 包的解析 只是不便于使用
    rationa1cuzz
        2
    rationa1cuzz  
       2021-05-12 11:37:06 +08:00
    不贴代码鬼知道你什么问题
    >>> response = requests.get("http://polygons.openstreetmap.fr/get_geojson.py?id=4738519")
    >>> response_json=response.json()
    >>> response_json.get("type")
    'GeometryCollection'
    wdc63
        3
    wdc63  
    OP
       2021-05-12 11:40:01 +08:00
    @009694
    @rationa1cuzz
    不好意思,我用的是 ironpython--py2.7,而且没法安装 requests
    代码:
    response = urllib2.urlopen(url, context=context, timeout=self.timeout)
    place_info = json.loads(response.read())
    出错:
    Runtime error (ValueErrorException): No JSON object could be decoded
    youngce
        4
    youngce  
       2021-05-12 11:54:57 +08:00
    - -上古代码,快跑
    wdc63
        5
    wdc63  
    OP
       2021-05-12 11:57:26 +08:00
    @youngce 平台限制。
    hasdream
        6
    hasdream  
       2021-05-12 11:59:46 +08:00
    使用 urllib2 也正常
    ```
    import json
    import urllib2
    resp = urllib2.urlopen("http://polygons.openstreetmap.fr/get_geojson.py?id=4738519")
    resp_json = json.load(resp)
    print resp_json.keys()
    ```
    no1xsyzy
        7
    no1xsyzy  
       2021-05-12 12:03:42 +08:00
    你先看看 response.read() 出来个啥吧。
    No JSON object could be decoded 基本就是空白字符串的意思。
    wdc63
        8
    wdc63  
    OP
       2021-05-12 13:51:22 +08:00 via Android
    @no1xsyzy 读出来就是打开这个网页显示的东西。str 类型
    no1xsyzy
        9
    no1xsyzy  
       2021-05-12 14:11:51 +08:00
    @wdc63 临时跑去下了一个 IronPython 2.7,没发现任何问题。

    >>> import json,urllib2
    >>> url="http://polygons.openstreetmap.fr/get_geojson.py?id=4738519"
    >>> response = urllib2.urlopen(url)
    >>> place_info = json.loads(response.read())
    >>> place_info['type']
    'GeometryCollection'
    julyclyde
        10
    julyclyde  
       2021-05-12 16:01:34 +08:00
    @wdc63 你把这个 response.read()单独输出看看不就知道了
    ALLROBOT
        11
    ALLROBOT  
       2021-05-12 18:39:25 +08:00
    json decode ?是解码问题?

    如果允许的话,请使用 python3.6,毕竟 python2 太老了
    ch2
        12
    ch2  
       2021-05-12 18:46:54 +08:00
    在网络编程的时候一定要注意先判断响应是否是 json 字符串,如果 http 请求异常,那么错误是在前一步而不是这一步
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   996 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 20:03 · PVG 04:03 · LAX 13:03 · JFK 16:03
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.