【发布时间】:2015-08-13 14:35:53
【问题描述】:
我能够从 url 中提取特定数据。
weather_data = r.get("http://api.openweathermap.org/data/2.5/weather?q=" + location)
json_weather = weather_data.text
weather_info = json.loads(json_weather)
print weather_info['coord']['lat']
print weather_info['coord']['lon']
这是在 API 上显示的内容
问题是,我怎样才能提取多个数据并将其放入一个列表(dict)中,而不是一个一个地提取它。
例如,我想将“纬度”、“经度”、“湿度”放在同一个列表中。
{u'base': u'stations',
u'clouds': {u'all': 40},
u'cod': 200,
u'coord': {u'lat': 51.51, u'lon': -0.13},
u'dt': 1439476222,
u'id': 2643743,
u'main': {u'humidity': 88,
u'pressure': 1012,
u'temp': 291.71,
u'temp_max': 293.15,
u'temp_min': 290.15},
u'name': u'London',
u'rain': {u'1h': 1.78},
u'sys': {u'country': u'GB',
u'id': 5091,
u'message': 0.0242,
u'sunrise': 1439440988,
u'sunset': 1439493986,
u'type': 1},
u'visibility': 9000,
u'weather': [{u'description': u'light intensity shower rain',
u'icon': u'09d',
u'id': 520,
u'main': u'Rain'}],
u'wind': {u'deg': 60, u'speed': 4.6}}
【问题讨论】:
-
不清楚你在问什么。在这个例子中,
weather_info是一个 Python 字典。 -
weather_info 是字典(或列表,但不太可能)。
-
JSON 对象 被default 转换为字典。
-
@larsks,我现在已经更详细地澄清了我的问题
标签: python json dictionary