[英]Accessing Nested arrays in JSON dictionary
嘿,我正在尝试在 json 字典内的数组中提取项目,但出现以下错误。 预先感谢您的帮助。
错误:
for r in data[p]['reportDetails']:
TypeError: unhashable type: 'dict'
代码片段如下:
def XeroExtractReports(cred_fp):
with open(cred_fp, 'r') as json_file:
data = json.load(json_file)
for p in data['credentials']:
client_id = p['clientId']
client_secret = p['clientSecret']
old_refresh_token = p['refreshToken']
for r in data[p]['reportDetails']:
get_url = 'https://api.xero.com/api.xro/2.0/' + r['reportName']
response = requests.get(get_url,
headers = {
'Authorization': 'Bearer ' + new_tokens[0],
'Xero-tenant-id': xero_tenant_id,
'Accept': 'application/json'
})
json_response = response.json()
print(json_response)
print('\n')
JSON 字典:
{
"credentials": [{
"clientName": "C1",
"clientId": "null",
"clientSecret": "null",
"redirectUrl": "http://localhost:8080/callback",
"scopes": "offline_access accounting.transactions.read",
"reportType": "null",
"refreshToken": "null",
"reportDetails": [
{
"reportName": "BankTransactions",
"reportFilename": "/BankTransactions.txt"
},
{
"reportName": "BankTransfers",
"reportFilename": "/BankTransfers.txt"
}
]
}]
}
您试图以错误的方式访问值
代替:
for r in data[p]['reportDetails']:
...
利用:
for r in p['reportDetails']:
...
您不需要使用data[p]
来获取reportDetails
的值。
当你设置p
时你已经得到了 dict
更改第二个循环:
for r in p['reportDetails']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.