我有来自 API 的 JSON 响应,其中列出了许可证到期时间。一个公司可能拥有多个许可证。 JSON 有很多我不需要的数据。我所需要的只是公司名称和许可证到期日期列表。 我的想法是提取所有公司名称并将它们用作字典键,然后值将是包含许可证到期日期的列表。我可以提取公司名称并创建一个以公司名称作为键的字典,但我不知道如何收集许可证日期并将其与特定公司关联起来。
以下是 API JSON 响应的示例。它被截断为仅包含来自同一“测试”公司的两条记录(我相信这足以证明结构),但完整的输出将有多个拥有多个许可证的公司。
因此“company_name”包含公司名称值,“expiration_time”包含许可证到期日期。
{
"data": [
{
"id": "62525384-e0b8-4a54-9600-13e0eccf1ac2",
"type": "licenses",
"attributes": {
"uuid": "62525384-e0b8-4a54-9600-13e0eccf1ac2",
"start_time": "2018-08-24 00:00:00 +0300",
"expiration_time": "2019-08-24 00:00:00 +0300",
"limit_hosts": 1,
"order_code": null,
"comments": null,
"organization_id": "07b05efa-6c5e-40d2-ab7f-348a19f02d71",
"organization": {
"data": {
"id": "07b05efa-6c5e-40d2-ab7f-348a19f02d71",
"type": "organizations",
"attributes": {
"uuid": "07b05efa-6c5e-40d2-ab7f-348a19f02d71",
"name": "tetete",
"parent_organization": 48,
"company_name": "tetete",
"created_at": "2018-08-24 15:24:14 +0300",
"excluded_from_licenses": true
}
}
}
}
},
{
"id": "90f6dd88-9260-47a2-82c1-45838659e781",
"type": "licenses",
"attributes": {
"uuid": "90f6dd88-9260-47a2-82c1-45838659e781",
"start_time": "2018-08-24 14:09:38 +0300",
"expiration_time": "2020-07-21 00:00:00 +0300",
"limit_hosts": 0,
"order_code": null,
"comments": null,
"organization_id": "8d9637af-fe33-48ab-b0b0-7728929c8171",
"organization": {
"data": {
"id": "8d9637af-fe33-48ab-b0b0-7728929c8171",
"type": "organizations",
"attributes": {
"uuid": "8d9637af-fe33-48ab-b0b0-7728929c8171",
"name": "tetete",
"parent_organization": 24,
"company_name": "tetete",
"created_at": "2018-08-24 14:09:38 +0300",
"excluded_from_licenses": true
}
}
}
}
}
],
"meta": {
"pagination": {
"total_count": 133,
"total_pages": 14,
"per_page": 10,
"current_page": 1
},
"total_licenses": 247
}
}
我现在的代码看起来像这样
def ExtractLicenseDetails():
LicenceLibrary = dict()
TempOrgList = []
for licence in lic["data"]:
TempOrgList.append(licence["attributes"]["organization"]["data"]["attributes"]["name"])
LicenceLibrary = dict.fromkeys(TempOrgList, list()) # no we create dictionary keys from non unique list of organisation, getting rid of duplicates in the process
for licence in lic["data"]:
LicenceLibrary[licence["attributes"]["organization"]["data"]["attributes"]["name"]].append(licence["attributes"]["expiration_time"])
print(LicenceLibrary)
不用说,它不起作用,但我的意思是错误的到期日期被添加到列表中。
你的代码看起来有点不直观,正如评论已经说过的那样,如果你提供一个 mre 以及代码的正确和实际输出,那就太好了,无论如何,这就是我会做的(+mre):
lic = {
"data": [
{
"id": "62525384-e0b8-4a54-9600-13e0eccf1ac2",
"type": "licenses",
"attributes": {
"uuid": "62525384-e0b8-4a54-9600-13e0eccf1ac2",
"start_time": "2018-08-24 00:00:00 +0300",
"expiration_time": "2019-08-24 00:00:00 +0300",
"limit_hosts": 1,
"order_code": None,
"comments": None,
"organization_id": "07b05efa-6c5e-40d2-ab7f-348a19f02d71",
"organization": {
"data": {
"id": "07b05efa-6c5e-40d2-ab7f-348a19f02d71",
"type": "organizations",
"attributes": {
"uuid": "07b05efa-6c5e-40d2-ab7f-348a19f02d71",
"name": "tetete",
"parent_organization": 48,
"company_name": "tetete",
"created_at": "2018-08-24 15:24:14 +0300",
"excluded_from_licenses": True
}
}
}
}
},
{
"id": "90f6dd88-9260-47a2-82c1-45838659e781",
"type": "licenses",
"attributes": {
"uuid": "90f6dd88-9260-47a2-82c1-45838659e781",
"start_time": "2018-08-24 14:09:38 +0300",
"expiration_time": "2020-07-21 00:00:00 +0300",
"limit_hosts": 0,
"order_code": None,
"comments": None,
"organization_id": "8d9637af-fe33-48ab-b0b0-7728929c8171",
"organization": {
"data": {
"id": "8d9637af-fe33-48ab-b0b0-7728929c8171",
"type": "organizations",
"attributes": {
"uuid": "8d9637af-fe33-48ab-b0b0-7728929c8171",
"name": "tetete",
"parent_organization": 24,
"company_name": "tetete",
"created_at": "2018-08-24 14:09:38 +0300",
"excluded_from_licenses": True
}
}
}
}
}
],
"meta": {
"pagination": {
"total_count": 133,
"total_pages": 14,
"per_page": 10,
"current_page": 1
},
"total_licenses": 247
}
}
expiration_dates = {}
for licence in lic["data"]:
name = licence["attributes"]["organization"]["data"]["attributes"]["company_name"] ## idk if you wanna use name or company_name, in your explanation it says company_name but you used name, DECIDE!!!!
expiration_time = licence["attributes"]["expiration_time"]
if name in expiration_dates.keys():
expiration_dates[name].append(expiration_time)
else:
expiration_dates[name] = [expiration_time]
print(expiration_dates)