Pretty Print JSON
本问题已经有最佳答案,请猛点这里访问。
我有一个python脚本,我想打印json输出,使其看起来像这样:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | { "authMode":"open", "enabled": false, "ipAssignmentMode":"NAT mode", "name":"Unconfigured SSID 14", "number": 13, "perClientBandwidthLimitDown": 0, "perClientBandwidthLimitUp": 0, "splashPage":"None", "ssidAdminAccessible": false }, { "authMode":"open", "enabled": false, "ipAssignmentMode":"NAT mode", "name":"Unconfigured SSID 15", "number": 14, "perClientBandwidthLimitDown": 0, "perClientBandwidthLimitUp": 0, "splashPage":"None", "ssidAdminAccessible": false } |
但是我的输出看起来是这样的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | {u'authMode': u'open', u'enabled': False, u'ipAssignmentMode': u'NAT mode', u'name': u'Unconfigured SSID 14', u'number': 13, u'perClientBandwidthLimitDown': 0, u'perClientBandwidthLimitUp': 0, u'splashPage': u'None', u'ssidAdminAccessible': False}, {u'authMode': u'open', u'enabled': False, u'ipAssignmentMode': u'NAT mode', u'name': u'Unconfigured SSID 15', u'number': 14, u'perClientBandwidthLimitDown': 0, u'perClientBandwidthLimitUp': 0, u'splashPage': u'None', u'ssidAdminAccessible': False}] |
我觉得有件简单的东西我错过了。这是我的代码:
1 2 3 4 | url ="https://dashboard.meraki.com/api/v0/networks/%s/ssids" % NETWORKID headers = {'X-Cisco-Meraki-API-Key': APIKEY} r = requests.get(url, headers=headers, allow_redirects=True) pprint (r.json()) |
我发现最简单的方法是使用
1 2 3 | r = requests.get(url, headers=headers, allow_redirects=True) import json print(json.dumps(r.json(), indent=2)) |
是的,这有点多余,因为您正在反序列化,然后再次序列化,但它可以工作。