You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从RIPE NCC API返回的嵌套字典中优雅取值?

简化RIPE NCC API返回嵌套字典的属性取值

从RIPE NCC RESTful API获取到创建Person Object的嵌套字典如下:

response_dict = {
   'link': {
       'type': 'locator', 
       'href': 'http://rest-test.db.ripe.net/test/person?dry-run=True'
   }, 
   'objects': {
       'object': [{
           'type': 'person', 
           'link': {
                'type': 'locator', 
                'href': 'https://rest-test.db.ripe.net/test/person/MT5-TEST'
            }, 
            'source': {
                'id': 'test'
            }, 
            'primary-key': {
                'attribute': [{
                    'name': 'nic-hdl', 
                    'value': 'MT5-TEST'
                 }]}, 
                    'attributes': { 
                        'attribute': [
                            {'name': 'person', 'value': 'Person test'}, 
                            {'name': 'address', 'value': 'Wegstraat 1'}, 
                            {'name': 'phone', 'value': '+31612345678'}, 
                            {'name': 'e-mail', 'value': 'person@company.com'}, 
                            {'link': {
                                'type': 'locator', 
                                'href': 'https://rest-test.db.ripe.net/test/mntner/TEST-DBM-MNT'}, 'name': 'mnt-by', 'value': 'TEST-DBM-MNT', 'referenced-type': 'mntner'}, 
                            {'name': 'nic-hdl', 'value': 'PT5-TEST'}, 
                            {'name': 'created', 'value': '2023-05-04T13:20:57Z'}, 
                            {'name': 'last-modified', 'value': '2023-05-04T13:20:57Z'}, 
                            {'name': 'source', 'value': 'TEST'}
                        ]
                    }
                }
                ]
                }, 
               'errormessages': {
                   'errormessage': [{
                       'severity': 'Info', 
                       'text': 'Dry-run performed, no changes to the database have been made'}
                    ]
                }, 
                'terms-and-conditions': {
                    'type': 'locator', 
                    'href': 'http://www.ripe.net/db/support/db-terms-conditions.pdf'
          }
      }

当前获取nic-hdl的写法为:

nic_hdl = response_dict['objects']['object'][0]['primary-key']['attribute'][0]['value']

这种方式可行但不够简洁,针对attributes中的多组name-value对,如何通过对应键值实现最优且最简洁的取值?


解决方案

核心思路是把attributes下的attribute列表转换为以name为键、value为值的字典,之后就能通过键名直接取值,无需逐层索引列表。

1. 转换为字典

先提取目标属性列表,再用字典推导式生成映射:

# 先获取person对象的attributes列表
person_attrs = response_dict['objects']['object'][0]['attributes']['attribute']

# 转换为name: value的字典
attr_dict = {item['name']: item['value'] for item in person_attrs}

2. 简洁取值

转换完成后,直接通过键名获取对应值:

# 获取姓名
person_name = attr_dict['person']
# 获取邮箱
email = attr_dict['e-mail']
# 获取创建时间
created_time = attr_dict['created']
# 获取mnt-by值
mnt_by = attr_dict['mnt-by']

3. 处理重复键名

如果存在多个同名的name(比如示例中primary-key和attributes里都有nic-hdl),可以根据需求选择:

  • 保留最后一个出现的值:字典推导式会自动覆盖之前的同名键
  • 保留所有值:改用列表存储,生成{name: [value1, value2,...]}的结构:
from collections import defaultdict

attr_multi_dict = defaultdict(list)
for item in person_attrs:
    attr_multi_dict[item['name']].append(item['value'])

# 获取所有nic-hdl值
all_nic_hdl = attr_multi_dict['nic-hdl']  # 结果为 ['PT5-TEST']

4. 封装复用

如果需要多次处理这类API返回,可以封装成函数:

def attrs_to_dict(attributes_list):
    return {item['name']: item['value'] for item in attributes_list}

# 使用示例
person_attr_dict = attrs_to_dict(response_dict['objects']['object'][0]['attributes']['attribute'])
phone = person_attr_dict['phone']

内容的提问来源于stack exchange,提问作者Beeelze

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 19:14:56