json 我如何在不同的立法者之间循环?

kknvjkwl  于 2022-11-19  发布在  其他
关注(0)|答案(1)|浏览(78)

我需要帮助使用Python来解析JSON文件。我已经使用了一个API来获取一个特定州的立法者列表,我想循环并找到一个特定的立法者(给定姓氏)。然后我想提取他们的CID。文件如下所示:

{
    "response": {
        "legislator": [
            {
                "@attributes": {
                    "cid": "N00029147",
                    "firstlast": "Andy Harris",
                    "lastname": "Harris",
                    "party": "R",
                    "office": "MD01",
                    "gender": "M",
                    "first_elected": "2010",
                    "exit_code": "0",
                    "comments": "",
                    "phone": "202-225-5311",
                    "fax": "202-225-0254",
                    "website": "http://harris.house.gov",
                    "webform": "https://harris.house.gov/contact-me/email-me",
                    "congress_office": "1533 Longworth House Office Building",
                    "bioguide_id": "H001052",
                    "votesmart_id": "19157",
                    "feccandid": "H8MD01094",
                    "twitter_id": "RepAndyHarrisMD",
                    "youtube_url": "https://youtube.com/RepAndyHarris",
                    "facebook_id": "AndyHarrisMD",
                    "birthdate": "1957-01-25"
                }
            },
            {
                "@attributes": {
                    "cid": "N00025482",
                    "firstlast": "Dutch Ruppersberger",
                    "lastname": "Ruppersberger",
                    "party": "D",
                    "office": "MD02",
                    "gender": "M",
                    "first_elected": "2002",
                    "exit_code": "0",
                    "comments": "",
                    "phone": "202-225-3061",
                    "fax": "202-225-3094",
                    "website": "http://ruppersberger.house.gov",
                    "webform": "http://ruppersberger.house.gov/contact-dutch/email-dutch",
                    "congress_office": "2416 Rayburn House Office Building",
                    "bioguide_id": "R000576",
                    "votesmart_id": "36130",
                    "feccandid": "H2MD02160",
                    "twitter_id": "Call_Me_Dutch",
                    "youtube_url": "https://youtube.com/ruppersberger",
                    "facebook_id": "184756771570504",
                    "birthdate": "1946-01-31"
                }
            }

`
我试着做一个for循环来解析这些值,但是没有用(顺便说一句,finance info是API提供的数据)。

finance_response_info = json.loads(financeInfo)
for v in finance_response_info["response"]:
    for a in finance_response_info["legislator"]:
        for b in finance_response_info["@attributes"][0]:
            if (b["lastname"] == lastName):
        candidateID = b["cid"]

`
然而,这并不起作用,我一直得到错误。我该如何正确地解析它呢?

siotufzp

siotufzp1#

# Go inside each dict inside the list
for legislator in finance_response_info['response']['legislator']:

    # If the lastname attribute inside the @attributes dict is equal to desired lastname
    if legislator['@attributes']['lastname'] == lastName:
        candidateID = legislator['@attributes']["cid"]

相关问题