删除json response python中的一些部分

问题描述 投票:-1回答:1

这是我的JSON响应,我想删除整个元标题部分。

{
    "meta": {
        "limit": 1000,
        "next": "https://cisco-demo.obsrvbl.com/api/v3/observations/all/?limit=1000&offset=1000",
        "offset": 0,
        "previous": null,
        "total_count": 1863020
    },
    "objects": [
        {
            "creation_time": null,
            "end_time": "2017-08-17T19:40:00Z",
            "id": 281,
            "observation_name": "External Port Scanner",
            "port_count": 251,
            "port_ranges": "0-1023",
            "resource_name": "port_scanner_external_v1",
            "scan_type": "inbound",
            "scanned_ip": "209.182.184.2",
            "scanned_ip_country_code": "US",
            "scanned_packets": 999,
            "scanner_ip": "130.126.24.53",
            "scanner_ip_country_code": "US",
            "scanner_packets": 997,
            "source": 109,
            "time": "2017-08-17T19:40:00Z"
        },
        {
            "creation_time": null,
            "end_time": "2017-08-17T19:50:00Z",
            "id": 304,
            "observation_name": "External Port Scanner",
            "port_count": 41,
            "port_ranges": "0-1023",
            "resource_name": "port_scanner_external_v1",
            "scan_type": "inbound",
            "scanned_ip": "209.182.184.2",
            "scanned_ip_country_code": "US",
            "scanned_packets": 152,
            "scanner_ip": "130.126.24.53",
            "scanner_ip_country_code": "US",
            "scanner_packets": 152,
            "source": 109,
            "time": "2017-08-17T19:50:00Z"
        },
python json api parsing http-delete
1个回答
0
投票
import json

with open('myfile.txt') as fp:
    my_dict = json.loads(fp.read())

try:
    del my_dict['meta']
except KeyError:
    pass

我假设您已经将JSON作为字典,在上面的代码中我只是从文本文件中加载它。你正在寻找的命令是del。将它放在try / catch块中是个好主意,以防你处理没有meta字段的字典,因为在这种情况下它会抛出一个KeyError并中断。

© www.soinside.com 2019 - 2024. All rights reserved.