我试图遍历从Microsoft Graph提取的JSON文件中的词典列表。我正在使用的代码能够将列表存储在变量中,但是我没有得到预期的9项,而是得到27。
我的代码是:
for i in range(len(json_data_list)):
try:
for key in json_data_list[i]:
headers.append(json_data_list[i]['internetMessageHeaders'])
except KeyError:
continue
i += 1
这是JSON数据的示例:
{
"@odata.context": "https://graph.microsoft.com/beta/$metadata#users('48d31887-5fad-4d73-a9f5-3c356e68a038')/messages(internetMessageHeaders)",
"@odata.nextLink": "https://graph.microsoft.com/beta/me/messages?$select=internetMessageHeaders&$skip=12",
"value": [
{
"@odata.etag": "W/\"CQAAABYAAAAiIsqMbYjsT5e/T7KzowPTAAEMTBu8\"",
"id": "AAMkAGVmMDEzMTM4LTZmYWUtNDdkNC1hMDZiLTU1OGY5OTZhYmY4OABGAAAAAAAiQ8W967B7TKBjgx9rVEURBwAiIsqMbYjsT5e-T7KzowPTAAAAAAEMAAAiIsqMbYjsT5e-T7KzowPTAAEMOXaXAAA="
},
{
"@odata.etag": "W/\"CQAAABYAAAAiIsqMbYjsT5e/T7KzowPTAAEMTBuN\"",
"id": "AAMkAGVmMDEzMTM4LTZmYWUtNDdkNC1hMDZiLTU1OGY5OTZhYmY4OABGAAAAAAAiQ8W967B7TKBjgx9rVEURBwAiIsqMbYjsT5e-T7KzowPTAAAAAAEMAAAiIsqMbYjsT5e-T7KzowPTAAEMOXaWAAA=",
"internetMessageHeaders": [
{
"name": "Received",
"value": "from DM6PR15MB2425.namprd15.prod.outlook.com (2603:10b6:102:2::22) by BYAPR15MB2423.namprd15.prod.outlook.com with HTTPS via CO2PR05CA0054.NAMPRD05.PROD.OUTLOOK.COM; Mon, 10 Sep 2018 21:52:40 +0000"
},
{
"name": "Received",
"value": "from CO1PR15CA0057.namprd15.prod.outlook.com (2603:10b6:101:1f::25) by DM6PR15MB2425.namprd15.prod.outlook.com (2603:10b6:5:8e::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.1122.19; Mon, 10 Sep 2018 21:52:38 +0000"
},
答案 0 :(得分:0)
意识到我不需要第二个for循环。
这按预期工作:
for i in range(len(jsonDataList)):
try:
headers.append(jsonDataList[i]['internetMessageHeaders'])
except KeyError:
continue
i += 1