你如何检查python中的空json字段

原文标题how do you check for empty json field in python

我正在进行 api 调用并将输出保存到 rest 变量。当我打印其余部分时,我将“实体”字段留空,如下所示:

print(resp)

{
  "totalCount": 0,
  "pageSize": 50,
  "entities": []
}

如果实体字段为空,我需要继续执行我的脚本并调用其他查询。我正在尝试这个:

if len(resp['entities'][0]['entityId'])==0:
        continue

这给了我错误:

1   if len(resp['entities'][0]['entityId'])==0:
    115         continue
    116 

IndexError: list index out of range

任何想法如何检查python中的空字段?

原文链接:https://stackoverflow.com//questions/71476962/how-do-you-check-for-empty-json-field-in-python

回复

我来回复
  • Tim Roberts的头像
    Tim Roberts 评论
    if not resp['entities']:
        print('list is empty')
    
    2年前 0条评论
  • dimas krisrianto的头像
    dimas krisrianto 评论
    if len(resp['entities'])==0:
        print('empty string')
    
    2年前 0条评论