我无法访问字典中的某些值,例如SID,RID和Active(在“数据”内部)。
{
"response":{
"Status":0,
"AsOfDate":"2018-11-22T18:22:48.7719635Z",
"ServiceName":"API",
"Version":"1.0.6836",
"Data":[
{
"UserName":"Automation",
"Name":" USER",
"Company":"SALE",
"LastName":"USER",
"Entitlements":{
"MAccess":true,
"EAccess":true,
"CAccess":true,
"TP":true,
"TQ":true,
"Active":false,
"TZ":true,
"C":true
},
"AltEmail":null,
"Status":"A",
"RID":"111111",
"IsActive":true,
"Name":"AUTOMATION",
"SID":"qYcuYD3DABU9ul2Rzg",
"WhiteListed":true,
"Email":"automation.user@something.com",
"Phone":"1234512345"
}
],
"EndTime":"2018-11-22T18:22:48.7719635Z",
"StartTime":"2018-11-22T18:22:48.7719635Z",
"Total":1
}
}
我对python比较陌生,这是我尝试过的方法,但是我一直遇到关键错误。请建议
json_obj = json.loads(res.content)
print(json_obj)
for p_id, p_info in json_obj.items():
print("\nPerson ID:", p_id)
for key in p_info:
print(key + ':', p_info[key])
print('@@@@@@@@@@',p_info['Active'])
print('@@@@@@@@@@',p_info['RID'])
答案 0 :(得分:0)
检查一下,它是总代码,但可以满足您的要求:
my_dict = {
"response":{
"Status":0,
"AsOfDate":"2018-11-22T18:22:48.7719635Z",
"ServiceName":"API",
"Version":"1.0.6836",
"Data":[
{
"UserName":"Automation",
"Name":" USER",
"Company":"SALE",
"LastName":"USER",
"Entitlements":{
"MAccess": True,
"EAccess": True,
"CAccess": True,
"TP": True,
"TQ": True,
"Active": True,
"TZ": True,
"C": True
},
"AltEmail": None,
"Status":"A",
"RID":"111111",
"IsActive": None,
"Name":"AUTOMATION",
"SID":"qYcuYD3DABU9ul2Rzg",
"WhiteListed": None,
"Email":"automation.user@something.com",
"Phone":"1234512345"
}
],
"EndTime":"2018-11-22T18:22:48.7719635Z",
"StartTime":"2018-11-22T18:22:48.7719635Z",
"Total":1
}
}
for value in my_dict.values():
for key, val in value.items():
if key == 'Data':
print(key)
for x, y in val[0].items():
if x == 'Entitlements':
print(x)
for z, v in y.items():
print('-', z, v)
else:
print('-',x, y)
else:
print(key, val)
和输出
Status 0
AsOfDate 2018-11-22T18:22:48.7719635Z
ServiceName API
Version 1.0.6836
Data
- UserName Automation
- Name AUTOMATION
- Company SALE
- LastName USER
Entitlements
- MAccess True
- EAccess True
- CAccess True
- TP True
- TQ True
- Active True
- TZ True
- C True
- AltEmail None
- Status A
- RID 111111
- IsActive None
- SID qYcuYD3DABU9ul2Rzg
- WhiteListed None
- Email automation.user@something.com
- Phone 1234512345
EndTime 2018-11-22T18:22:48.7719635Z
StartTime 2018-11-22T18:22:48.7719635Z
Total 1
答案 1 :(得分:0)
您可以通过直接访问json加载路径来访问
p_info['response']['Data'][0]['Entitlements']['Active']
p_info['response']['Data'][0]['RID']
p_info['response']['Data'][0]['SID']