python - 检查 python 中的 Json 数据是否为 ​​none

标签 python django django-rest-framework

这是我的 json 可能是我们在 django rest 框架中请求的数千条记录数据。

{
 "a":1,
 "b":2,
 "c":3,
 .....
}

if request.data['a'] == '':
   return JsonResponse({'error':1})
elif request.data['b'] == '':
   return JsonResponse({'error':1})
elif .....
   .......
else:
   ......

如果... else,我想检查数据是否像上面那样为空。我们可以添加 if else 条件,但这不可能检查 if else 的所有数据请建议我在 python 中检查这行代码的最佳方法是什么。

最佳答案

如果您使用 json 模块通过 json.loads 解析 json 响应文本,那么您可以像下面的 dict 一样直接访问它。

import json
json_data = json.loads(json_text)

for mykey in json_data:
    if json_data[mykey] = "":
        return JsonResponse({'error':1})
    else:
        pass

关于python - 检查 python 中的 Json 数据是否为 ​​none,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56765803/

相关文章:

django - django admin中的表单字段描述

python - 解析 DRF 请求主体 : TypeError the JSON object must be str, 而不是 'bytes'

python - 如何在 Dict 中查找重复值并使用这些值打印键

python - 移动友好测试脚本

python - Django Rest Framework (DRF) 如何根据 query_params 设置分页类?

python - Django Rest框架在POST请求中创建或更新

django-rest-framework 返回创建的对象但字段较少

python - 如何知道可以为 Python 3 中的平台设置的最大递归深度?

python - 无效的 token 和无效的语法

带有逻辑括号和优先顺序的 Django 模板 IF 条件