Python 我需要来自JSON的字典中的字典中的特定数据

Python 我需要来自JSON的字典中的字典中的特定数据,python,api,Python,Api,我只希望从这个JSON API响应中提取catergory ID 我目前有以下代码,其中dictstr是返回的JSON print(dictstr['Store']['CustomCategories']['CustomCategory'][0:-1]) 这将打印出以下JSON: { 'CategoryID':'20004239012', 'Name':'Home Decor', 'Order':'1', 'ChildCategory':{

我只希望从这个JSON API响应中提取catergory ID

我目前有以下代码,其中
dictstr
是返回的JSON

print(dictstr['Store']['CustomCategories']['CustomCategory'][0:-1])
这将打印出以下JSON:

{  
    'CategoryID':'20004239012',
    'Name':'Home Decor',
    'Order':'1',
    'ChildCategory':{  
        'CategoryID':'20215926012',
        'Name':'Furniture',
        'Order':'1'
    }
},
{  
    'CategoryID':'20004240012',
    'Name':'Christmas Decorations',
    'Order':'2'
},
我需要的理想输出只是
CatergoryID
Name
键/值,如下所示:

['1', 'Other', '20004239012', 'Home Decor', '20215926012', 'Furniture', 
'20004240012', 'Christmas Decorations', '20270732012', 'Candle stands', 
'20270902012', 'Fireplace tools']

假设您只需要深入一层,您可以:

代码: 怎么用? 通过迭代列表,您可以提取字段的元组(
d.get(…)
),然后使用
sum()
组合元组,最后删除任何
None
(缺少)元素

测试代码: 结果:
我想我们可能需要一个示例响应或服务器代码。欢迎使用StackOverflow。请阅读并遵循帮助文档中的发布指南。比如
[e['CategoryID']for e in d['CustomCategories']['CustomCategories']]
获得
['1','11400059015','11400060015','11400061015']
谢谢更新帖子,不要被否决票吓坏了-发布好的问题是一项技能,你会很快学会的,你在@johnashu工作了吗?我在上面的评论中发布了一行代码,应该可以使用<代码>[e['CategoryID']对于d['Store']['CustomCategories']['CustomCategories']].给了我
['1','11400059015','11400060015','11400061015'].
cids = [x for x in sum(
    [(d.get('CategoryID'), d.get('ChildCategory', {}).get('CategoryID'))
     for d in data], ()) if x]
data = [{
    'CategoryID': '20004239012',
    'Name': 'Home Decor',
    'Order': '1',
    'ChildCategory': {
        'CategoryID': '20215926012',
        'Name': 'Furniture',
        'Order': '1'
    }
},
{
    'CategoryID': '20004240012',
    'Name': 'Christmas Decorations',
    'Order': '2'
},
]

cids = [x for x in sum(
    [(d.get('CategoryID'), d.get('ChildCategory', {}).get('CategoryID'))
     for d in data], ()) if x]

print(cids)
['20004239012', '20215926012', '20004240012']