使用
isinstance()
要检查类型,它将返回布尔值
y = {
"attributes": [
{
"name": "test"
}
]
}
# p2 = json.dumps(y)
if 'attributes' not in y:
print("not found")
else:
print(y['attributes'])
print(isinstance(y['attributes'], list))
if isinstance(y['attributes'], list):
print('its a list')
print(len(y['attributes']))
print("found")
输出:
[{'name': 'test'}]
True
its a list
1
found