Python json 解析问题,在线等

2021-05-12 11:18:47 +08:00
 wdc63

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

2063 次点击
所在节点    Python
12 条回复
009694
2021-05-12 11:32:23 +08:00
首先没有展示任何代码 几乎很难有人能凭空想象来帮你 。 其次这是 geojson 不过并不影响 json 包的解析 只是不便于使用
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
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
2021-05-12 11:54:57 +08:00
- -上古代码,快跑
wdc63
2021-05-12 11:57:26 +08:00
@youngce 平台限制。
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
2021-05-12 12:03:42 +08:00
你先看看 response.read() 出来个啥吧。
No JSON object could be decoded 基本就是空白字符串的意思。
wdc63
2021-05-12 13:51:22 +08:00
@no1xsyzy 读出来就是打开这个网页显示的东西。str 类型
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
2021-05-12 16:01:34 +08:00
@wdc63 你把这个 response.read()单独输出看看不就知道了
ALLROBOT
2021-05-12 18:39:25 +08:00
json decode ?是解码问题?

如果允许的话,请使用 python3.6,毕竟 python2 太老了
ch2
2021-05-12 18:46:54 +08:00
在网络编程的时候一定要注意先判断响应是否是 json 字符串,如果 http 请求异常,那么错误是在前一步而不是这一步

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/776427

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX