我已将 json 数据加载到api_result
变量中。现在我需要提取特定字段(name
,surname
等city
)。我应该如何验证它们是否存在?
api_result = json.loads(some_json_data)
if api_result.get('name'):
# do something with name
if api_result.get('surname'):
# do something with surname
if api_result.get('city'):
# do something with city
这是正确的方法吗?看起来太复杂了。如果找不到值,有什么方法可以获取空值?