从json dict访问dict对象



我有一个json,其中包括一个dict和一个列表。我只想循环dict对象并停止循环。

json_res = {
"abc": "123",
"Students": [
{ 
"Sub1": {
"Name":"Amit Goenka" 
},
"Sub2": {
"Major":"Physics" , 
"Name2":"Smita Pallod" 
},
"Sub3": {
"Major2":"Chemistry" , 
"Name3":"Rajeev Sen" , 
"Major3":"Mathematics" 
}
},
[
[
"Name",
0,
"Student",
1
]
]
]
}

以下是我尝试过的:

for data in json_res['Students']:
val = data.get('Sub3')

结果:

{
"Major2":"Chemistry" , 
"Name3":"Rajeev Sen" , 
"Major3":"Mathematics" 
} 

并且失败。我只想考虑来自";Sub1";直到";Sub3";。

您需要添加isistance来检查dict.

for data in json_res['Students']:
if isinstance(data, dict):
val = data.get('Sub3')
print(val)

最新更新