Python 如何对特定字段的所有键值对遍历json对象?

Python 如何对特定字段的所有键值对遍历json对象?,python,json,api,Python,Json,Api,我有这个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, "las

我有这个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

代码:

导入json
导入csv
将open('../resources/web_json_test.json','r')作为文件_1:
json_res:dict=json.load(文件_1.read())
打开('../out/json_to_csv_out.csv','w',换行符='')作为文件_1:
csv_writer=csv.DictWriter(文件_1,字段名=[‘操作系统’、‘标记’、‘ip地址’]))
csv_writer.writeheader()
对于json_res[‘资产’]中的curr:
csv_writer.writerow(
{'operating_system':curr[“operating_system”],'tags':curr[“tags”],'ip_address':curr[“ip_address”]})
输出:

操作系统、标签、ip地址
“Microsoft-Windows-Windows Server 2008 R2,企业版-SP1”,“['Windows Server','DO-DO SPG BOM']”,10.10.10.1


“但我的方式没有产生我想要的预期产出。”你能更具体一点吗?此外,我尝试解析您在上面共享的JSON示例,但我发现一个解码器错误。JSON示例中似乎缺少一些字符。此外,您的
print('operation system…
行看起来没有正确缩进,尽管我不知道这是您的代码中的问题还是添加到帖子中的方式中的问题。@AlexanderCécile我打印出来的只是每个特定字段
print('operating_system:'+json_format['assets')[0]的第一个索引['operating_system']
只打印第一次出现的内容,我想查看整个JSON(请参阅我的答案:)我的最新答案对你有用吗?谢谢你的回复!很有帮助alot@Xfactor不客气!如果它满意地回答了您的问题,您可以。出于好奇,我确实有一个问题。如果我想将此打印语句写入csv,我是否必须将特定字段及其出现的情况分开?@Xfactor您想将e操作系统,每个系统的标签和ip?是的,例如,我在csv文件“Microsoft-Windows-Windows Server 2008 R2,Enterprise Edition-SP1”中得到以下输出:“'Windows Server','DO-DO SPG BOM',ip地址:10.10.10.1”,标签:['Windows Server','DO-DO SPG BOM']| ip地址:10.10.10.1```