如何修复:查询json d时,列表索引必须是整数,而不是str

2024-10-01 22:29:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在执行一个get请求(json输出):

url = "https://"+user+":"+api+"@api.softlayer.com/rest/v3.1/SoftLayer_Virtual_Guest/"+id+"/getSoftwareComponents"

querystring = {"objectMask":"mask[passwords]"}

headers = {
   'cache-control': "no-cache"
    }
response = requests.get( url, headers=headers, params=querystring)
pwdata = response.json()
print (json.dumps(pwdata, indent=2))

输出:

[
  {
    "manufacturerLicenseInstance": "", 
    "passwords": [
      {
        "username": "test", 
        "modifyDate": "2018-05-11T21:52:54+09:00", 
        "softwareId": 36440389, 
        "id": 38400901, 
        "port": null, 
        "password": "xxxxxxxx", 
        "createDate": "2018-05-11T21:52:54+09:00", 
        "software": null
      }
    ], 
    "hardwareId": null, 
    "id": 36440389
  }, 
  {
    "manufacturerLicenseInstance": "", 
    "passwords": [
      {
        "username": "nabe", 
        "modifyDate": "2018-05-11T21:52:54+09:00", 
        "softwareId": 36440391, 
        "id": 38402045, 
        "port": null, 
        "password": "xxxxxxx", 
        "createDate": "2018-05-11T21:52:54+09:00", 
        "software": null
      }
    ], 
    "hardwareId": null, 
    "id": 36440391
  }
]

从上面的输出中,我尝试用以下方法过滤id中的passwords[]值:

pwdid = [pwd['id'] for pwd in pwdata['passwords']]

或者

pwdid = pwdata['passwords'][4]['id']

它们都给出了list indices must be integers, not str错误

显然,我使用listpwd['id']的方式有些不正确。你知道吗


Tags: apiidjsonurlcachegetresponseusername
2条回答

板球是对的,你有一个错误,因为你用一个字符串子集列表。试试这个:

[ [ pwd["id"] for pwd in instance["passwords"]] for instance in pwdata]

从“数据”中提取密码ID

data = [
    {
        "manufacturerLicenseInstance": "",
        "passwords": [
            {
                "username": "test",
                "modifyDate": "2018-05-11T21:52:54+09:00",
                "softwareId": 36440389,
                "id": 38400901,
                "port": None,
                "password": "xxxxxxxx",
                "createDate": "2018-05-11T21:52:54+09:00",
                "software": None
            }
        ],
        "hardwareId": None,
        "id": 36440389
    },
    {
        "manufacturerLicenseInstance": "",
        "passwords": [
            {
                "username": "nabe",
                "modifyDate": "2018-05-11T21:52:54+09:00",
                "softwareId": 36440391,
                "id": 38402045,
                "port": None,
                "password": "xxxxxxx",
                "createDate": "2018-05-11T21:52:54+09:00",
                "software": None
            }
        ],
        "hardwareId": None,
        "id": 36440391
    }
]
ids = [pwd['id'] for sublist in [entry['passwords'] for entry in data] for pwd in sublist]
print(ids)

输出

[38400901, 38402045]

易于阅读的版本

ids = []
password_dict_list = [entry['passwords'] for entry in data]
for entry in password_dict_list:
    for sub_entry in entry:
        ids.append(sub_entry['id'])
print(ids)

输出

[38400901, 38402045]

相关问题 更多 >

    热门问题