我有一個從api中提取json數據的腳本,我希望它在提取所述數據後解碼並選擇要存儲的標記一個分貝。現在我只需要讓腳本返回特定的值即可。這是腳本看起來像,之前我試圖解碼它。如何從json數據庫中提取特定節點並返回它的值
import requests
def call():
payload = {'apikey':'945e8e8499474b7e8d2bc17d87191bce', 'zip' : '47120'}
bas_url = 'http://congress.api.sunlightfoundation.com/legislators/locate'
r = requests.get(bas_url, params = payload)
grab = r.json()
return grab
這就是我被告知,試圖讓那些特定的標記返回
return [{key: value for key, value in result.items() if key in ("twitter_id", "office")} for result in jsonData]
,但返回的錯誤。有什麼建議麼?
'results':[{'twitter_id':'RepToddYoung','ocd_id':'ocd-division/country:us/state:in/cd:9','oc_email':'[email protected] 'org','middle_name':'C.','votesmart_id':120345,'first_name':'Todd','youtube_id':'RepToddYoung','last_name':'Young','bioguide_id':'Y000064' ,'區':9,'暱稱':無,'office':'1007 Longworth House Office Building','term_start':'2015-01-06','thomas_id':'02019','派對':' R','in_office':True,'title':'Rep','govtrack_id':'412428','crp_id':'N00030670','term_end':'2017-01-03','chamber' 'state_name':'Indiana','fax':'202-226-6866','phone':'202-225-5315','gender':'M','fec_ids':['H0IN09070 '','國家':'IN','網站':'http://toddyoung.house.gov','name_suffix':None,'icpsr_id':21133,'facebook_id':'186203844738421','contact_form':'https://toddyoungforms.house.gov/give-me-your-opinion','birthday' :'1972 -08-24'},{'twitter_id':'SenDonnelly','ocd_id':'ocd-division/country:us/state:in','oc_email':'[email protected]','middle_name ':None,'lis_id':'S356','first_name':'Joe','youtube_id':'sendonnelly','last_name':'Donnelly','bioguide_id':'D000607','區' '暱稱':無,'辦公室':'720哈特參議院辦公樓','state_rank':'junior','thomas_id':'01850','term_start':'2013-01-03','派對': 'D','in_office':True,'title':'Sen','govtrack_id':'412205','crp_id':'N00026586','term_end':'2019-01-03','chamber': 'state_name':'Indiana','fax':'202-225-6798','phone':'202-224-4814','gender':'M','senate_class':1, 'fec_ids':['H4IN02101','S2IN00091'],'state':'IN','votesmart_id':34212,'website':'http://www.donnelly.senate.gov','name_suffix':None,'icpsr_id':20717,'facebook_id ':'168059529893610','contact_form':'http://www.donnelly.senate.gov/contact/email-joe','birthday':'1955-09-28'},{'twitter_id':'SenDanCoats','ocd_id':'ocd-division /國家/地區:us/state:in','oc_email':'[email protected]','middle_name':'Ray','lis_id':'S212','first_name':'Daniel','youtube_id' :'SenatorCoats','last_name':'外套','bioguide_id':'C000542','區':無,'暱稱':無,'辦公室':'493羅素參議院辦公樓','state_rank':' ''''''''''''''''''''','''''','''''''','party':'R','in_office':True,'title':'Sen','govtrack_id' 402675','crp_id':'N00003845','term_end':'2017-01-03','chamber':'senate','state_name':'Indiana','傳真':'202-228-1820' ,'phone':'202-224-5623','gender':'M','senate_class':3,'fec_ids':['S0IN00053'],'state':'IN','votesmart_id':53291 ,'網站':'http://www.coats.senate.gov','name_suffix':None,'icpsr_id':14806,'facebook_id':'180671148633644','contact_form':'http://www.coats.senate.gov/contact/','birthday':'1943-05-16'}] }
發佈已解碼JSON的示例。(即,將'print(grab)'添加到您的程序中並將其樣本複製粘貼到您的問題中。) –
請停止一次又一次發佈相同的問題。如果你不停下來,就會採取行動。這是爲了你自己的利益。 –