我正在尝试通过python中的循环和条件语句来获取json文件的内容。
这是JSON文件内容:
{
"count": 3,
"result": [
{
"type": "first",
"first": {
"_links": {
"self1": {
"href": "/c6a5c1db-cf6e-4b12-9944-a1b6451963d4"
},
"self2": {
"href": "https://example.com"
},
"ver": {
"href": "https://example1.com"
}
},
"id": "c6a5c1db-cf6e-4b12-9944-a1b6451963d4",
"tags": []
}
},
{
"type": "year",
"year": {
"month": {
"api1": {
"href": "https://Ap1.com"
},
"api2": {
"href": "FETCH-CONTENT"
},
"api3": {
"href": "https://Ap3.com"
},
"api4": {
"href": "https://Ap4.com"
}
},
"idProvider": {
"id": "sfsmkfmskf",
"name": "Apikey"
},
"tags": []
}
},
{
"type": "year",
"year": {
"month": {
"api1": {
"href": "https://Ap11.com"
},
"api2": {
"href": "FETCH-CONTENT-1"
},
"api3": {
"href": "https://Ap13.com"
},
"api4": {
"href": "https://Ap14.com"
}
},
"identityProvider": {
"id": "00000000-0000-0000-0000-000000000000",
"name": ""
},
"tags": []
}
}
]
}
我正在尝试从第一个对象访问self2
href
,并从第二个和第三个对象访问api3
href_1
。如果没有第一个对象,我将能够访问api3
href_1
并打印所需的密钥。
但是,当我通过传递完整文件来打印内容时,它无法打印内容。因为它没有为所有对象获得相同的键名称。
要打印
print([item['year']['month']['api3'] for item in data['result']])
如果json文件不包含结果数组的第一个元素,则以上行将打印所需的数据。当我添加结果数组的第一个元素时,它不会给出任何结果。
答案 0 :(得分:2)
使用以下语句忽略具有"type": "first"
的项目。有用。
print([item['year']['month']['api3'] for item in data['result'] if "year" in item])
您可以在下面的示例中看到它。
>>> data = {
... "count": 3,
... "result": [
... {
... "type": "first",
... "first": {
... "_links": {
... "self1": {
... "href": "/c6a5c1db-cf6e-4b12-9944-a1b6451963d4"
... },
... "self2": {
... "href": "https://example.com"
... },
... "ver": {
... "href": "https://example1.com"
... }
... },
... "id": "c6a5c1db-cf6e-4b12-9944-a1b6451963d4",
... "tags": []
... }
... },
... {
... "type": "year",
... "year": {
... "month": {
... "api1": {
... "href": "https://Ap1.com"
... },
... "api2": {
... "href": "FETCH-CONTENT"
... },
... "api3": {
... "href": "https://Ap3.com"
... },
... "api4": {
... "href": "https://Ap4.com"
... }
... },
... "idProvider": {
... "id": "sfsmkfmskf",
... "name": "Apikey"
... },
... "tags": []
... }
... },
... {
... "type": "year",
... "year": {
... "month": {
... "api1": {
... "href": "https://Ap11.com"
... },
... "api2": {
... "href": "FETCH-CONTENT-1"
... },
... "api3": {
... "href": "https://Ap13.com"
... },
... "api4": {
... "href": "https://Ap14.com"
... }
... },
... "identityProvider": {
... "id": "00000000-0000-0000-0000-000000000000",
... "name": ""
... },
... "tags": []
... }
... }
... ]}
>>>
>>> print([item['year']['month']['api3'] for item in data['result'] if "year" in item])
[{'href': 'https://Ap3.com'}, {'href': 'https://Ap13.com'}]
>>>
答案 1 :(得分:2)
您只需要选择正确类型的记录即可,例如在理解中使用if
,例如:
print([item['year']['month']['api3'] for item in data['result']
if item['type'] == 'year'])
data = {
"count": 3,
"result": [
{
"type": "first",
"first": {
"_links": {
"self1": {
"href": "/c6a5c1db-cf6e-4b12-9944-a1b6451963d4"
},
"self2": {
"href": "https://example.com"
},
"ver": {
"href": "https://example1.com"
}
},
"id": "c6a5c1db-cf6e-4b12-9944-a1b6451963d4",
"tags": []
}
},
{
"type": "year",
"year": {
"month": {
"api1": {
"href": "https://Ap1.com"
},
"api2": {
"href": "FETCH-CONTENT"
},
"api3": {
"href": "https://Ap3.com"
},
"api4": {
"href": "https://Ap4.com"
}
},
"idProvider": {
"id": "sfsmkfmskf",
"name": "Apikey"
},
"tags": []
}
},
{
"type": "year",
"year": {
"month": {
"api1": {
"href": "https://Ap11.com"
},
"api2": {
"href": "FETCH-CONTENT-1"
},
"api3": {
"href": "https://Ap13.com"
},
"api4": {
"href": "https://Ap14.com"
}
},
"identityProvider": {
"id": "00000000-0000-0000-0000-000000000000",
"name": ""
},
"tags": []
}
}
]
}
print([item['year']['month']['api3'] for item in data['result']
if item['type'] == 'year'])
[
{'href': 'https://Ap3.com'},
{'href': 'https://Ap13.com'}
]
答案 2 :(得分:0)
尝试了几种组合后,下面的代码可以正常工作。
import json
with open('C:\python\examplee.json', 'r+') as fr:
data = json.load(fr)
for item in data['result']:
if item['type']=='year':
print([item['year']['month']['api3']['href']])
else:
print([item['first']['_links']['self2']['href']])