【发布时间】:2012-10-23 09:30:02
【问题描述】:
我正在尝试从 Python (2.7) 中的 Open Weather maps API 的 JSON 数据中提取所需的数据! JSON 文件看起来像这样。
{u'calctime': 0.0331,
u'cnt': 2,
u'cod': u'200',
u'list': [{u'clouds': {u'all': 75},
u'coord': {u'lat': 45.551109, u'lon': 18.69389},
u'distance': 0.001,
u'dt': 1351870200,
u'id': 3193935,
u'main': {u'humidity': 93,
u'pressure': 1008,
u'temp': 287.15,
u'temp_max': 291.18,
u'temp_min': 287.15},
u'name': u'Osijek',
u'weather': [{u'description': u'broken clouds',
u'icon': u'04d',
u'id': 803,
u'main': u'Clouds'}],
u'wind': {u'deg': 70, u'speed': 4.6}},
{u'clouds': {u'all': 75},
u'coord': {u'lat': 45.568611, u'lon': 18.61389},
u'distance': 6.526,
u'dt': 1351870200,
u'id': 3187657,
u'main': {u'humidity': 93,
u'pressure': 1008,
u'temp': 285.43,
u'temp_max': 287.15,
u'temp_min': 282.15},
u'name': u'Visnjevac',
u'weather': [{u'description': u'light intensity shower rain',
u'icon': u'09d',
u'id': 520,
u'main': u'Rain'}],
u'wind': {u'deg': 70, u'speed': 4.6}}],
u'message': u'Model=GFS-OWM, '}
这是两个地理点的天气数据。我知道如何使用 json 包从两个点(例如湿度或温度)中提取单个数据,但问题是我无法从不同的地理点提取多个相同类型的数据。在此示例中,我想提取/打印这些点的名称(osijek、visnjevac)。
有没有办法使用 json 编码器/解码器?
【问题讨论】:
-
为什么不能提取多个数据点?你试过什么,它是怎么失败的?我非常怀疑这是 JSON 解码器问题。
-
另外,当您说“看起来像这样”时,您是说您发布的代码是 JSON文件,还是您在解码 JSON? (因为它不是 JSON——
u''是 unicode 字符串的 Python 语法,而不是 JSON。)
标签: python json weather-api