我有这个json响应
{ "assets": [
{
"id": 518447,
"created_at": "2019-09-10T10:13:38Z",
"priority": 10,
"operating_system": "Microsoft - Windows - Windows Server 2008 R2, Enterprise Edition - SP1",
"notes": null,
"last_booted_at": null,
"primary_locator": "external_id",
"locator": "1112359",
"vulnerabilities_count": 22,
"status": "active",
"last_seen_time": "2019-09-08T16:00:17Z",
"network_ports": [
{
"id": 33550493,
"port_number": 180,
"extra_info": "",
"hostname": null,
"name": "HTTP",
"ostype": "",
"product": "JBoss EAP",
"protocol": "tcp",
"state": "open",
"version": "4.2.3.GA"
},
{
"id": 33550494,
"port_number": 100,
"extra_info": "",
"hostname": null,
"name": "SNMP",
"ostype": "",
"product": null,
"protocol": "udp",
"state": "open",
"version": null
},
],
"tags": [
"Windows Server",
"DO - DO SPG BOM"
],
"owner": null,
"urls": {
"vulnerabilities": ""
},
"ip_address": "10.10.10.1",
"database": null,
"hostname": null,
"fqdn": null,
"netbios": null,
"application": null,
"file": null,
"mac_address": null,
"ec2": null,
"url": null,
"external_id": "1112359",
"ipv6": null,
"asset_groups": [
{
"id": 4,
"name": "0 Global - All"
},
{
"id": 204,
"name": "DO - All"
},
{
"id": 417,
"name": "Do - All"
}
]
}
我已经尝试过通过第一个索引[0]来实现这一点,但我知道还有更好的方法
import request
import json
url = 'https://thisismyurl.com/assets/'
token = 'blahblah'
headers = {'X-Risk-Token': token, 'Accept': 'application/json'}
response = requests.get(url,headers=headers)
print(response.status_code)
json_format = json.loads(response.text)
for a in json_format['assets']:
for key, value in json_format:
print('operating_system : ' + json_format['assets'][0]['operating_system'] + ' , ' + 'ip_address : ' + json_format['assets'][0]['ip_address'] + 'tags : ' + json_format['assets'][0]['tags'])
但我的方式并没有产生我想要的预期产出。你知道吗
我只想浏览整个json,找到每个操作系统、ip地址和标记
我想要的输出是:
"operating_system": "Microsoft - Windows - Windows Server 2008 R2, Enterprise Edition - SP1", "tags": "Windows Server" "DO - DO SPG BOM" , "ip_address": "10.10.10.1".
我如何使用Python呢?你知道吗
代码中有多个部分可能是导致错误的原因。我写了一些代码,给出了适当的结果,唯一的区别是我从一个文件中读取JSON。你知道吗
代码:
输出:
相关问题 更多 >
编程相关推荐