简体   繁体   English

如何从JSON输出中提取特定数据?

[英]How to pull specific data from JSON output?

I've been working on a simple weather web app using flask and python. 我一直在使用flask和python开发一个简单的天气Web应用程序。

One of the routes I have in my main.py file is the weather route. 我的main.py文件中包含的路线之一是天气路线。 It has a function that calls the weather api, and prints the json output. 它具有一个调用weather api的功能,并输出json输出。

My problem is pulling specific data out of the json output. 我的问题是从json输出中拉出特定数据。 This is what the output looks like: 输出如下所示:

{'base': 'stations',
 'clouds': {'all': 90},
 'cod': 200,
 'coord': {'lat': 40.73, 'lon': -73.99},
 'dt': 1557824237,
 'id': 5128581,
 'main': {'humidity': 93,
          'pressure': 1009,
          'temp': 43.93,
          'temp_max': 46,
          'temp_min': 42.01},
 'name': 'New York',
 'sys': {'country': 'US',
         'id': 4026,
         'message': 0.0144,
         'sunrise': 1557826807,
         'sunset': 1557878678,
         'type': 1},
 'visibility': 12874,
 'weather': [{'description': 'mist', 'icon': '50n', 'id': 701, 'main': 'Mist'},
             {'description': 'light intensity drizzle',
              'icon': '09n',
              'id': 300,
              'main': 'Drizzle'}],
 'wind': {'deg': 20, 'speed': 8.05}}

I used pprint to make it a little more readable. 我使用了pprint使其更具可读性。 What I am trying to do is pull the "description" data by creating a python dictionary. 我想要做的是通过创建python字典来提取“描述”数据。

weather = {
    'city': city,
    'temperature': r['main']['temp'],
    'description': r['weather'][1]['description'],
    'icon': r['weather'][1]['icon'],
    }

When I print weather, I expect it to print out description: light intensity drizzle , icon: 09n , id:300 , but I get a key error. 当我打印天气时,我希望它能打印出description: light intensity drizzleicon: 09nid:300 ,但是出现一个关键错误。 I don't get a key error when I use "0" instead of "1" but I am trying to pull the second description data, not the first. 当我使用“ 0”而不是“ 1”时,没有出现关键错误,但是我试图提取第二个描述数据,而不是第一个。 Any suggestions? 有什么建议么?

Try calling the weather key then use indices to get into the list, then do gets on the items you want. 尝试调用weather键,然后使用索引进入列表,然后获取所需的项目。

data = {'base': 'stations',
 'clouds': {'all': 90},
 'cod': 200,
 'coord': {'lat': 40.73, 'lon': -73.99},
 'dt': 1557824237,
 'id': 5128581,
 'main': {'humidity': 93,
          'pressure': 1009,
          'temp': 43.93,
          'temp_max': 46,
          'temp_min': 42.01},
 'name': 'New York',
 'sys': {'country': 'US',
         'id': 4026,
         'message': 0.0144,
         'sunrise': 1557826807,
         'sunset': 1557878678,
         'type': 1},
 'visibility': 12874,
 'weather': [{'description': 'mist', 'icon': '50n', 'id': 701, 'main': 'Mist'},
             {'description': 'light intensity drizzle',
              'icon': '09n',
              'id': 300,
              'main': 'Drizzle'}],
 'wind': {'deg': 20, 'speed': 8.05}}


print(data.get('weather')[0].get('description'))
print(data.get('weather')[0].get('icon'))
print(data.get('weather')[0].get('id'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM