在for循环中比较python中JSON对象的值

在for循环中比较python中JSON对象的值,python,python-3.x,list,dictionary,Python,Python 3.x,List,Dictionary,我有一个JSON格式的对象,它是从API主体从用户那里接收的。在Python中存储并检查其类型时,会显示dict。但是dict中的键是作为一个集合存储的 x = {'test': {'shipmentInfo': {'Ready Date', 'Ready Time', 'Delivery Date', 'Service Level'}}} 我将字典的所有键存储在一个列表中,如下所示 check_list = ["test", "shipmentInfo"

我有一个JSON格式的对象,它是从API主体从用户那里接收的。在Python中存储并检查其类型时,会显示dict。但是dict中的键是作为一个集合存储的

x = {'test': {'shipmentInfo': {'Ready Date', 'Ready Time', 'Delivery Date', 'Service Level'}}}
我将字典的所有键存储在一个列表中,如下所示

check_list = ["test", "shipmentInfo", "Ready Date","Ready Time","Delivery Date","Service Level"]
我正在写一个简单的条件来检查字典中给出的每个键是否都出现在我的列表中。如果任何钥匙不存在,则应说明钥匙丢失

missing = [field for field in x if field not in check_list]
   if len(missing) == 0:
       print("All values are entered")
   else:
       [print(f"Missing value: {field}") for field in missing]
我的情况的问题是,它只检查字典中是否存在“test”。它不是检查我需要的主键(“准备日期”、“准备时间”、“交付日期”、“服务级别”)。 如果我从列表中删除一个值,如交货日期

("Ready Date","Ready Time","Service Level")
我使用的逻辑将给出这个结果

All values are entered

如何获取(“准备日期”、“准备时间”、“交付日期”、“服务级别”)并将其与我的列表进行比较?

{'Ready Date'、'Ready Time'、'Delivery Date'、'Service Level'}
构成一个集合,它们不是内部字典的键,但是仍然可以检查原始词典中是否存在这些代码:

已实现的
dictionary\u to\u list
函数将原始dictionary
x
放平到一个列表中,该列表包含列表中的所有键和值

x = {'test': {'shipmentInfo': {'Ready Date', 'Ready Time', 'Delivery Date', 'Service Level'}}}
check_list = ["test", "shipmentInfo", "Ready Date","Ready Time","Delivery Date","Service Level"]


def dictionary_to_list_helper(d, l):
    for k, v in d.items():
        l.append(k)
        if isinstance(v, list) or isinstance(v, set):
            for item in v:
                l.append(item)
        elif isinstance(v, dict):
            dictionary_to_list_helper(v, l)

def dictionary_to_list(d):
    l = []
    dictionary_to_list_helper(d, l)
    return l

missing = [field for field in dictionary_to_list(x) if field not in check_list]
if len(missing) == 0:
   print("All values are entered")
else:
   [print(f"Missing value: {field}") for field in missing]

您最里面的项目
{'Ready Date','Ready Time','Delivery Date','Service Level'}
集合
,而不是
目录
。@user32882我如何访问这些项目?我是python的初学者,无法编写您无法编写的部分。。。。一个集合并不意味着被订阅。。。一个更好的问题是,为什么在那里有一个集合?@user32882:这是JSON格式的响应,我通过API从用户那里收到,并像那样存储。我必须检查列表中是否存在每个元素。