我试图从一个以JSON对象形式返回数据的API中提取数据。我有一个API调用,它返回如下所示的数据(为了简洁起见,这里缩短了)
"status":"OK",
"copyright":"Copyright (c) 2017 Pro Publica Inc. All Rights Reserved.",
"results":[
{
"num_results": 10,
"offset": 0,
"bills": [
{
"bill_id": "hr2739-113",
"bill_type": "hr",
"number": "H.R.2739",
"bill_uri": "https://api.propublica.org/congress/v1/113/bills/hr2739.json",
"title": "Efficient Use of Government Spectrum Act of 2013",
"sponsor_title": "Rep.",
"sponsor_id": "M001163",
"sponsor_name": "Doris Matsui",
"sponsor_state": "CA",
"sponsor_party": "D",
"sponsor_uri": "https://api.propublica.org/congress/v1/members/M001163.json",
"gpo_pdf_uri": "http://www.gpo.gov/fdsys/pkg/BILLS-113hr2739ih/pdf/BILLS-113hr2739ih.pdf",
"congressdotgov_url": "https://www.congress.gov/bill/113th-congress/house-bill/2739",
"govtrack_url": "https://www.govtrack.us/congress/bills/113/hr2739",
"introduced_date": "2013-07-18",
"committees": "House Armed Services Committee",
"committee_codes": ["HSAS","HSIF"],
"subcommittee_codes": ["HSAS26","HSIF16"],
"primary_subject": "Science, Technology, Communications",
"summary_short": "Efficient Use of Government Spectrum Act of 2013 - Directs the Federal Communications Commission (FCC), within three years after enactment of the Middle Class Tax Relief and Job Creation Act of 2012, to: (1) reallocate electromagnetic spectrum between the frequencies from 1755 to 1780 megahertz (currently, such frequencies are occupied by the Department of Defense [DOD] and other federal agencies); and (2) as part of the competitive bidding auctions required by such Act, grant new initial lic...",
"latest_major_action_date": "2013-08-29",
"latest_major_action": "Referred to the Subcommittee on Intelligence, Emerging Threats & Capabilities."
},
{
"bill_id": "hr3355-113",
"bill_type": "hr",
"number": "H.R.3355",
"bill_uri": "https://api.propublica.org/congress/v1/113/bills/hr3355.json",
"title": "Reducing Employer Burdens, Unleashing Innovation, and Labor Development Act of 2013",
"sponsor_title": "Rep.",
"sponsor_id": "G000558",
"sponsor_name": "Brett Guthrie",
"sponsor_state": "KY",
"sponsor_party": "R",
"sponsor_uri": "https://api.propublica.org/congress/v1/members/G000558.json",
"gpo_pdf_uri": "http://www.gpo.gov/fdsys/pkg/BILLS-113hr3355ih/pdf/BILLS-113hr3355ih.pdf",
"congressdotgov_url": "https://www.congress.gov/bill/113th-congress/house-bill/3355",
"govtrack_url": "https://www.govtrack.us/congress/bills/113/hr3355",
"introduced_date": "2013-10-28",
"committees": "House Armed Services Committee",
"primary_subject": "Economics and Public Finance",
"summary_short": "Reducing Employer Burdens, Unleashing Innovation, and Labor Development Act of 2013 - Expresses the sense of Congress that increasing the competitiveness of U.S. manufacturers will strengthen the national economy. Title I: Investing in America's Workforce - Investing in America's Workforce Act - Amends the Workforce Investment Act of 1998 to require state or local workforce investment systems to use youth activities funds allocated to a local area for programs that provide training, which may...",
"latest_major_action_date": "2014-01-24",
"latest_major_action": "Referred to the Subcommittee on Intelligence, Emerging Threats & Capabilities."
},
使用Python,我尝试遍历数据并返回所有值,如下所示:
import requests
import json
r = requests.get({url and credentials here}).text
resp = json.loads(r)
for bill in resp['results']['bills']:
name = bill['results']['bills']['title']
type = item['results']['bills']['bill_type']
print(name, type)
然而,每当我试图运行这个,我得到
TypeError: list indices must be integers or slices, not str
为什么我不能使用列表索引作为str?有很多使用str的例子
在json数据中
results
是一个dict列表。 所以首先迭代结果。 然后对每个结果遍历bills以检索bill的名称和类型。你知道吗bill
是一个json对象,它没有results
或bills
作为字符串索引。请尝试以下操作:json中的“bills”是一个对象列表(例如
"bills" : [
-左括号是正方形,表示一个列表)。你知道吗您必须使用整数来访问列表中的元素,因此您可以说
例如,访问第一个账单。你知道吗
但是,您的代码在循环中有点混乱,
bill
变量将包含每个账单的信息,因此您可以直接引用键,例如在第一个结果中循环账单变量“name”和“type”将只包含循环块期间每个账单的详细信息。你知道吗
可以嵌套循环以循环所有结果
相关问题 更多 >
编程相关推荐