2017-10-20 211 views
0

初级调用字典内的字典的值(读取JSON文件)

我有此JSON文件:

{ 'created_at':“星期三09月27 1点19分39秒+ 0000 2017','id':912849180741087232,'id_str':'912849180741087232','text':“RT @TheRickWilson:我发现点击扑克已经生效,尖叫声中有特殊情况。\ n \ n是的,是因为Trump ca ...“,”source“:'Twitter for iPhone','截断':False,'in_reply_to_status_id':无,'in_reply_to_status_id_str':无,'in_reply_to_user_id':无,'in_reply_to_user_id_str':无,'in_reply_to_screen_name':无,'user':{'id':66914769,'id_str':'66914769','name':'Kathy','screen_name':'mydoggigi','location':'Earth','url' 'description':'爱情政治,孙子& PSU #StillWithHer #NotMyPresident苏珊莎兰登,格伦格林沃尔德,乔尔奥斯汀和乔斯卡伯勒阻止! #TheResistance','translator_type':'none','protected':False,'verified':False,'followers_count':5878,'friends_count':5973,'listed_count':143,'favourites_count':110285,'' '','','','','','','','',' 'lang':'en','contributors_enabled':False,'is_translator':False,'profile_background_color':'C0DEED','profile_background_image_url':'http://abs.twimg.com/images/themes/theme1/bg.png','profile_background_image_url_https':'https://abs.twimg.com/images/themes/theme1/bg.png','profile_background_tile':False, 'profile_link_color':'1DA1F2','profile_sidebar_border_color':'C0DEED','profile_sidebar_fill_color':'DDEEF6','profile_text_color':'333333','profile_use_background_image':True,'profile_image_url':'http://pbs.twimg.com/profile_images/903412377424732160/NqCfPFiB_normal.jpg','profile_image_url_https': 'https://pbs.twimg.com/profile_images/903412377424732160/NqCfPFiB_normal.jpg','profile_banne r_url':'https://pbs.twimg.com/profile_banners/66914769/1504225271','default_profile':True,'default_profile_image':False,'following':None,'follow_request_sent':None,'notifications':None},'geo':None,'coordinates':None, 'place':None,'contributors':None,'retweeted_status':{'created_at':'Wed Sep 27 01:08:45 +0000 2017','id':912846439964987392,'id_str':'912846439964987392','文字':“我发现点滴已经生效,尖叫着AL的特殊情况。\ n \ n是的,这是因为特朗普无法交付。 '','source':'Twitter for Android','truncated':False,'in_reply_to_status_id':None,'in_reply_to_status_id_str':None,'in_reply_to_user_id':None,'in_reply_to_user_id_str':None,'in_reply_to_screen_name':None, 'user':{'id':19084896,'id_str':'19084896','name':'Rick Wilson','screen_name':'TheRickWilson','location':'Florida and points beyond','url' :'http://facebook.com/therickwilson','description':'GOP Media Guy,爸爸,丈夫,飞行员,猎人,作家,我做广告和做政治,每日野兽专栏作家,一切特朗普接触死亡。','translator_type':'none','protected':False,'verified':True,'followers_count':238578,'friends_count':3518,'listed_count':4235,'favourites_count':48094,'statuses_count': 250609,'created_at':'Fri Jan 16 20:50:17 +0000 2009','utc_offset':-14400,'time_zone':'America/New_York','geo_enabled':False,'lang':'en' ,'contributors_enabled':False,'is_translator':False,'profile_background_color':'1A1B1F','profile_background_image_url':'http://pbs.twimg.com/profile_background_images/220716353/Firefox_Wallpaper.jpg','profile_background_image_url_https':'https://pbs.twimg.com/profile_background_images/220716353/Firefox_Wallpaper.jpg','profile_background_tile':True,'profile_link_color':'445555' 'profile_image_url_https':'https://pbs.twimg.com/profile_images/813585115934658560/gnuRozoD_normal.jpg','profile_banbar_color':' :'https://pbs.twimg.com/profile_banners/19084896/1504722796','default_profile':False,'default_profile_image': False,'following':None,'follow_request_sent':None,'notifications':None},'geo':None,'coordinates':None,'place':None,'贡献者':None,'is_quote_status':False ,'quote_count':5,'reply_count':50,'retweet_count':100,'favorite_count':456,'entities':{'hashtags':[],'urls':[],'user_mentions':[] ,'symbols':[]},'favitedited':False,'retweeted':False,'filter_level':'low','lang':'en'},'is_quote_status':False,'quote_count':0, 'reply_count':0,'retweet_count':0,'favorite_count':0,'entities':{'hashtags':[],'urls':[],'user_mentions':[{'screen_name':'TheRickWilson' ,'name':'Rick Wilson','id':19084896,'id_str':'19084896','indices':[3,17]]],'symbols':[]},'favorited':False, 'retweeted':False,'filter_level':'low','lang':'en','timestamp_ms':'1506475179263'}

我能弄清楚如何调用部分json文件中像这样:

with open('trump1.json') as data_file: 
     #making a dictionary of the json document so I can call values 
    data = json.load(data_file) 
    #print(data) 
    #when the tweet was made 
    data["created_at"] 
    print(data["created_at"]) 
    #content of the tweet 
    data["text"] 
    print(data["text"]) 

但我无法弄清楚如何调用的重要信息,如

data["name"] 

以来的字典的一个内(见JSON文件的加粗部分)

有谁知道如何做到这一点? 谢谢!

+0

你有什么尝试?关于这个和外面的问题有很多问题。 – Antimony

回答

1

您可以访问嵌套的字典,像这样:

data['user']['name'] 
+0

非常感谢! – Brit

0

如果你肯定知道“用户”关键是要出现在你的JSON文件总是,那么你可以先做:

user = data['user'] 

如果您知道用户肯定会有一个名字,那么你可以阅读“名称”字段是这样的:

return user['name'] 

上述两个步骤合并为一个,你可以这样做:

return data['user']['name'] 

如果你希望“用户”字段有时不存在,也有不同的方法可以处理它:

# Using the dictionary object's .get() method to be safe. 
# Here, returning None by default. 
# This isn't a good idea if None is an expected value for name. 
return data.get('user', {}).get('name') 

# Surrounding the dictionary lookup with try ... except 
# Of course, KeyError is just one of the ways this can go wrong. 
try: 
    return data['user']['name'] 
except KeyError: 
    # Handle error 

# Look before you leap. Verify that 'user' and 'name' fields are present. 
# Though, this results in two dictionary lookups. 
if 'user' in data: 
    user = data['user'] 
    if 'name' in user: 
     return user['name']