从Python中JSON的嵌套字典和数组中提取特定值

2024-09-30 06:17:47 发布

您现在位置:Python中文网/ 问答频道 /正文

这是对我之前提出的一个问题的更具体的补充。基本上,我试图从处理GET请求后收到的JSON文本块中提取特定数据。在限制到只包含项目之后(通过使用目录项()方法),我只剩下以下JSON数据(有很多,但这只是一个示例):

[('response',
  {'count': 84,
   'users': [{'location_id': 123456,
                   'acx_audit': None,
                   'flash_backup_url': u'',
                   'flash_backup_url_secure': u'',
                   'flash_click_variable': None,
                   'folder': None,
                   'format': u'url-json',
                   'height': 1,
                   'id': 36619222,
                   'is_prohibited': False,},
             {'location_id': 5556667,
                   'acx_audit': None,
                   'flash_backup_url': u'',
                   'flash_backup_url_secure': u'',
                   'flash_click_variable': None,
                   'folder': None,
                   'format': u'url-json',
                   'height': 1,
                   'id': 4567777,
                   'is_prohibited': False,}

我尝试提取的数据是这些对象中每一个的“id”号(而不是“location\u id”),并将收集到的所有这些号放入一个数组中。不过,我有点纠结,因为我不习惯使用像这样的大JSON数据结构,其中有相互嵌套的字典和数组。我是否也需要运行一个循环来完成这个任务?你知道吗

任何帮助都将不胜感激,因为我现在被困住了。谢谢。你知道吗


Tags: 数据noneidjsonformaturllocationaudit
1条回答
网友
1楼 · 发布于 2024-09-30 06:17:47

可以按以下方式进行(涉及一些严格的条件检查,以避免引发异常)

inp=[('response',
  {'count': 84,
   'users': [{'location_id': 123456,
                   'acx_audit': None,
                   'flash_backup_url': u'',
                   'flash_backup_url_secure': u'',
                   'flash_click_variable': None,
                   'folder': None,
                   'format': u'url-json',
                   'height': 1,
                   'id': 36619222,
                   'is_prohibited': False},
             {'location_id': 5556667,
                   'acx_audit': None,
                   'flash_backup_url': u'',
                   'flash_backup_url_secure': u'',
                   'flash_click_variable': None,
                   'folder': None,
                   'format': u'url-json',
                   'height': 1,
                   'id': 4567777,
                   'is_prohibited': False},
                {'location_id': 5556667,
                   'acx_audit': None,
                   'flash_backup_url': u'',
                   'flash_backup_url_secure': u'',
                   'flash_click_variable': None,
                   'folder': None,
                   'format': u'url-json',
                   'height': 1,
                   'id': 4567777,
                   'is_prohibited': False},
            {'location_id': 5556667,
                   'acx_audit': None,
                   'flash_backup_url': u'',
                   'flash_backup_url_secure': u'',
                   'flash_click_variable': None,
                   'folder': None,
                   'format': u'url-json',
                   'height': 1,
                   'id': 4567777,
                   'is_prohibited': False}]})]
output=[]
for tup_el in inp:
    for el in tup_el:
        if type(el) is dict:
            if "users" in el.keys():
                if type(el["users"]) is list:
                    for obj in el["users"]:
                        if "id" in obj.keys():
                            output.append(obj['id'])
print output

相关问题 更多 >

    热门问题