首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从Python中的API json响应中获取特定数据

如何从Python中的API json响应中获取特定数据
EN

Stack Overflow用户
提问于 2021-05-30 12:52:38
回答 1查看 439关注 0票数 0

我是一位对网络自动化非常感兴趣的网络工程师。我是起诉python自动化的初学者。

最近,我编写了一个代码,将一个GET API查询发送到一个设备,并获取网络对象的详细信息,它给了我很多我实际上不需要的信息。

我想知道如何从json响应中解析或获取特定数据。

例如,来自以下响应:

代码语言:javascript
复制
{'links': {'self': 'https://fmcrestapisandbox.cisco.com/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f/object/networks?offset=0&limit=25&expanded=true'}, 'items': [{'links': {'self': 'https://fmcrestapisandbox.cisco.com/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f/object/networks/005056BB-0B24-0ed3-0000-893353264061', 'parent': 'https://fmcrestapisandbox.cisco.com/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f/object/networkaddresses'}, 'type': 'Network', 'value': '10.0.0.0/8', 'overridable': True, 'description': 'test api put wih postman ', 'id': '005056BB-0B24-0ed3-0000-893353264061', 'name': '10.0.0.0_8', 'metadata': {'timestamp': 1602974037876, 'lastUser': {'name': 'akankshu_a'}, 'domain': {'name': 'Global', 'id': 'e276abec-e0f2-11e3-8169-6d9ed49b625f'}, 'ipType': 'V_4', 'parentType': 'NetworkAddress'}}, {'links': {'self': 'https://fmcrestapisandbox.cisco.com/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f/object/networks/005056BB-0B24-0ed3-0000-893353272647', 'parent': 'https://fmcrestapisandbox.cisco.com/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f/object/networkaddresses'}, 'type': 'Network', 'value': '10.10.210.0/24', 'overridable': False, 'description': ' ', 'id': '005056BB-0B24-0ed3-0000-893353272647', 'name': '10.10.210.0_24', 'metadata': {'timestamp': 1598427295586, 'lastUser': {'name': 'akankshu_a'}, 'domain': {'name': 'Global', 'id': 'e276abec-e0f2-11e3-8169-6d9ed49b625f'}, 'ipType': 'V_4', 'parentType': 'NetworkAddress'}}

我只想拿这个:

代码语言:javascript
复制
'type': 'Network', 'value': '10.10.210.0/24', 'overridable': False, 'description': ' ', 'name': '10.10.210.0_24'

帮帮忙吧。

关健

附加注释:

我使用了以下代码来获取所需的结果:

嗨,比尔,我做了以下工作以获得所需的输出,但是,我需要知道如何将其格式化为json,以便您可以通过API.........import json将它推到设备上。

"https://fmcrestapisandbox.cisco.com/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f/object/networks/005056BB-0B24-0ed3-0000-893353264061",=“{”链接:{“自我”:"config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f/object/networks?offset=0&limit=25&expanded=true"}“,”项“:{”链接“:{”自“:”父“:"https://fmcrestapisandbox.cisco.com/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f/object/networkaddresses"},"type":"Network","value":"10.0.0.0/8","overridable":"True","description":"test api put wih postman ","id":"005056BB-0B24-0ed3-0000-893353264061","10.0.0.0_8",“元数据”:{“时间戳”:1602974037876,"lastUser":{"name":"akankshu_a"},“域”:{“名称”:“全局”,"id":"e276abec-e0f2-11e3-8169-6d9ed49b625f"},"ipType":"V_4","parentType":“NetworkAddress”},{“链接”:{"self":V_4“父”:"https://fmcrestapisandbox.cisco.com/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f/object/networkaddresses"},"type":"Network","value":"10.10.210.0/24","overridable":"False","description":“","id":"005056BB-0B24-0ed3-0000-893353272647","10.10.210.0_24",“元数据”:{“时间戳”:1598427295586,"lastUser":{“名称”:"akankshu_a"},“域”:{“名称”:“全局”,"id":"e276abec-e0f2-11e3-8169-6d9ed49b625f"},"ipType":"V_4","parentType":“NetworkAddress”}}

json_obj = json.loads(data_txt)

Json_obj.pop(“链接”)

项目= json_obj

Pop_item中val的pop_item='links','id',‘元数据’:对于范围(0,2)中的val2 (0,2):json_obj‘’items‘.pop(Val)print(json_obj’item‘)

但是,我想知道如何将其转换为json格式,以便我可以使用API方法将其推送到另一个设备。

EN

回答 1

Stack Overflow用户

发布于 2021-05-30 20:13:02

首先,您可以使用像JSON Parser Online这样的工具来确保为解析而正确格式化JSON。这是有用的张贴在这里,因为它给人们一个可复制的例子。

完成之后,您可以使用Python的内置json模块将JSON文本解析为可以导航的对象。

代码语言:javascript
复制
import json

data_txt = """
{"links": {"self": "https://fmcrestapisandbox.cisco.com/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f/object/networks?offset=0&limit=25&expanded=true"}, "items": [{"links": {"self": "https://fmcrestapisandbox.cisco.com/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f/object/networks/005056BB-0B24-0ed3-0000-893353264061", "parent": "https://fmcrestapisandbox.cisco.com/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f/object/networkaddresses"}, "type": "Network", "value": "10.0.0.0/8", "overridable": "True", "description": "test api put wih postman ", "id": "005056BB-0B24-0ed3-0000-893353264061", "name": "10.0.0.0_8", "metadata": {"timestamp": 1602974037876, "lastUser": {"name": "akankshu_a"}, "domain": {"name": "Global", "id": "e276abec-e0f2-11e3-8169-6d9ed49b625f"}, "ipType": "V_4", "parentType": "NetworkAddress"}}, {"links": {"self": "https://fmcrestapisandbox.cisco.com/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f/object/networks/005056BB-0B24-0ed3-0000-893353272647", "parent": "https://fmcrestapisandbox.cisco.com/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f/object/networkaddresses"}, "type": "Network", "value": "10.10.210.0/24", "overridable": "False", "description": " ", "id": "005056BB-0B24-0ed3-0000-893353272647", "name": "10.10.210.0_24", "metadata": {"timestamp": 1598427295586, "lastUser": {"name": "akankshu_a"}, "domain": {"name": "Global", "id": "e276abec-e0f2-11e3-8169-6d9ed49b625f"}, "ipType": "V_4", "parentType": "NetworkAddress"}}]}
"""

json_obj = json.loads(data_txt)

现在,您有了一个具有“链接”部分和“项”部分的对象。您可以跳过链接,因为您要查找的所有信息都在项目中。

代码语言:javascript
复制
items = json_obj["items"]

从这里开始,您可以使用索引直接访问所需的项,也可以在项目列表上循环以从所有(两者)获取信息。

代码语言:javascript
复制
item = items[1] # just get the second item

# loop over all of the items
for item in items:
    print("Type:", item["type"])
    print("Value:", item["value"])
    print("Overridable:", item["overridable"])
    print("Description:", item["description"])
    print("Name:", item["name"])
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67761358

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档