Python json 解析问题,在线等 - V2EX
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 2691 次点击
    这是一个创建于 1616 天前的主题,其中的信息可能已经有所发展或是发生改变。

    当使用 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 JSN 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     Solana     1119 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 26ms UTC 23:11 PVG 07:11 LAX 16:11 JFK 19:11
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86