python - 如何在 python 列表中创建和比较 JSON 值

标签 python json python-requests

尝试构建和比较从 JSON REST API 提取的项目列表,似乎有一种更简单的方法可以将整个 json 响应转换为

我有来自 REST API 的 JSON 数据,我想从中提取并比较数据对。该代码显示了从响应中提取的两个值,但我想重复执行此操作,并且仅显示尚未返回的值。

>>> response = requests.get('http://192.168.2.3:8081/api/session/wifi', auth=('user', 'pass'))
>>> json_response = response.json()
>>> #parse the json and get list of mac and channels
... for each in json_response['aps']:
...     channel = each['channel']
...     mac = each['mac']
...     print channel, mac
...
11 74:83:c2:1a:d2:74
11 18:e8:29:bb:16:39
11 f0:f2:49:0b:9b:e8
6 c0:7c:d1:b8:14:28
6 84:34:97:b4:0f:a1
11 76:83:c2:1a:d2:74
11 f0:f2:49:e2:2c:e8
1 1a:e8:29:9d:1e:63
11 1a:e8:29:9b:16:39
11 1a:e8:29:9d:1f:16
11 18:e8:29:9d:1f:16
1 18:e8:29:9d:1e:63

我希望有一种更简单的方法可以使用 JSON 请求对象来完成此操作。

这是源 JSON:

{
    "aps": [{
            "received": 0,
            "handshake": false,
            "clients": [],
            "vendor": "Ubiquiti Networks Inc.",
            "encryption": "WPA2",
            "hostname": "CoreWireless",
            "authentication": "PSK",
            "wps": {},
            "sent": 1875183,
            "alias": "",
            "mac": "18:e8:29:9d:1e:63",
            "meta": {
                "values": {}
            },
            "frequency": 2412,
            "ipv4": "0.0.0.0",
            "ipv6": "",
            "rssi": -63,
            "first_seen": "2019-09-18T09:56:09.938447-06:00",
            "cipher": "TKIP",
            "channel": 1,
            "last_seen": "2019-09-18T12:45:50.489350663-06:00"
        }, {
            "received": 0,
            "handshake": false,
            "clients": [],
            "vendor": "",
            "encryption": "WPA2",
            "hostname": "<hidden>",
            "authentication": "PSK",
            "wps": {},
            "sent": 0,
            "alias": "",
            "mac": "1a:e8:29:9b:16:39",
            "meta": {
                "values": {}
            },
            "frequency": 2462,
            "ipv4": "0.0.0.0",
            "ipv6": "",
            "rssi": -73,
            "first_seen": "2019-09-18T09:56:12.473447-06:00",
            "cipher": "CCMP",
            "channel": 11,
            "last_seen": "2019-09-18T12:45:49.718002705-06:00"
        }
    ]
}

最佳答案

你能实现如下所示的缓存吗?

cache = set()
for each in json_response['aps']:
    channel = each['channel']
    mac = each['mac']
    if (channel, mac) not in cache:
        print channel, mac
        cache.add((channel, mac))

关于python - 如何在 python 列表中创建和比较 JSON 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57999316/

相关文章:

python - 从 Python 脚本调用 Pyramid View

python - selenium.common.exceptions.WebDriverException : Message: 'library' executable may have wrong permissions for ChromeDriver

java - 使@JsonTypeInfo 属性可选

python - 带分页的 Django API 列表 - 页面不是 JSON 可序列化的

python - 机器人框架: send binary data in POST request body with

python - 有没有办法找到结束 token

python - 使用 python 中给定的 key 生成 AES 256 GCM key

javascript - 解析 JSON 对象以获取唯一记录

python - 在 Python 2.6 中发送 TLS 1.2 请求

python - 错误 :cannot use a string pattern on a bytes-like object