使用python

时间:2017-11-10 06:09:27

标签: python json parsing

我正在尝试与API交互并遇到访问嵌套对象的问题。下面是我正在使用的示例json输出。

{
    "results": [
        {
            "task_id": "22774853-2b2c-49f4-b044-2d053141b635",
            "params": {
                "type": "host",
                "target": "54.243.80.16",
                "source": "malware_analysis"
            },
            "v": "2.0.2",
            "status": "success",
            "time": 227,
            "data": {
                "details": {
                    "as_owner": "Amazon.com, Inc.",
                    "asn": "14618",
                    "country": "US",
                    "detected_urls": [],
                    "resolutions": [
                        {
                            "hostname": "bumbleride.com",
                            "last_resolved": "2016-09-15 00:00:00"
                        },
                        {
                            "hostname": "chilitechnology.com",
                            "last_resolved": "2016-09-16 00:00:00"
                        }
                    ],
                    "response_code": 1,
                    "verbose_msg": "IP address in dataset"
                },
                "match": true
            }
        }
    ]
}

我能够访问的最深的是返回太多的数据部分....理想情况下我只是尝试访问as_owner,asn,country,detected_urls,resolution

当我尝试访问详细信息/响应代码等时,我会得到一个KeyError。我的嵌套json比其他提到的Q更深入,我尝试了这种逻辑。

以下是我当前的代码段,感谢任何帮助!

import requests
import json
headers = {
   'Content-Type': 'application/json',
}

params = (
   ('wait', 'true'),
)

data = '{"target":{"one":{"type": "ip","target": "54.243.80.16", "sources": ["xxx","xxxxx"]}}}'

r=requests.post('https://fakewebsite:8000/api/services/intel/lookup/jobs', headers=headers, params=params, data=data, auth=('apikey', ''))
parsed_json = json.loads(r.text)
#results = parsed_json["results"]
for item in parsed_json["results"]:
     print(item['data'])

1 个答案:

答案 0 :(得分:0)

您只需要正确转换为转换后的JSON即可。然后,您可以轻松地遍历要获取的密钥列表,因为它们都在“详细信息”字典中。

import json

raw = '''\
{
    "results": [
        {
            "task_id": "22774853-2b2c-49f4-b044-2d053141b635",
            "params": {
                "type": "host",
                "target": "54.243.80.16",
                "source": "malware_analysis"
            },
            "v": "2.0.2",
            "status": "success",
            "time": 227,
            "data": {
                "details": {
                    "as_owner": "Amazon.com, Inc.",
                    "asn": "14618",
                    "country": "US",
                    "detected_urls": [],
                    "resolutions": [
                        {
                            "hostname": "bumbleride.com",
                            "last_resolved": "2016-09-15 00:00:00"
                        },
                        {
                            "hostname": "chilitechnology.com",
                            "last_resolved": "2016-09-16 00:00:00"
                        }
                    ],
                    "response_code": 1,
                    "verbose_msg": "IP address in dataset"
                },
                "match": true
            }
        }
    ]
}
'''

parsed_json = json.loads(raw)

wanted = ['as_owner', 'asn', 'country', 'detected_urls', 'resolutions']

for item in parsed_json["results"]:
    details = item['data']['details']
    for key in wanted:
        print(key, ':', json.dumps(details[key], indent=4))
    # Put a blank line at the end of the details for each item
    print()    

<强>输出

as_owner : "Amazon.com, Inc."
asn : "14618"
country : "US"
detected_urls : []
resolutions : [
    {
        "hostname": "bumbleride.com",
        "last_resolved": "2016-09-15 00:00:00"
    },
    {
        "hostname": "chilitechnology.com",
        "last_resolved": "2016-09-16 00:00:00"
    }
]

BTW,当您使用requests获取JSON数据时,无需使用json.loads:您可以使用返回的.json对象的request方法访问转换后的JSON而不是使用其.text属性。

这是上面代码的主循环的更强大版本。它只是忽略了任何丢失的键。我之前没有发布此代码,因为额外的if测试会使效率略低,而且我不知道 可能会丢失

for item in parsed_json["results"]:
    if not 'data' in item:
        continue
    data = item['data']
    if not 'details' in data:
        continue
    details = data['details']
    for key in wanted:
        if key in details:
            print(key, ':', json.dumps(details[key], indent=4))
    # Put a blank line at the end of the details for each item
    print()