如何以专栏或漂亮的方式打印json

时间:2019-05-08 19:10:34

标签: python json

我写这段代码 导入请求

auth_token='eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJkZWZhdWx0Iiwia3ViZX$
hed = {'Authorization': 'Bearer ' + auth_token}
data = {'app' : 'aaaaa'}

url =  'https://203.0.113.106:6443/api'
a=response = requests.get(url, headers=hed,verify=False)
print(a.json())

收到

{u'serverAddressByClientCIDRs': [{u'clientCIDR': u'0.0.0.0/0', u'serverAddress': u'203.0.113.106:6443'}], u'kind': u'APIVersions', u'versions': [u'v1']}

但是我想将其打印为

{
  "kind": "APIVersions",
  "versions": [
    "v1"
  ],
  "serverAddressByClientCIDRs": [
    {
      "clientCIDR": "0.0.0.0/0",
      "serverAddress": "203.0.113.106:6443"
    }
  ]

我该怎么办?

1 个答案:

答案 0 :(得分:1)

您可以使用标准的json模块重新编码JSON:

import json

data = response.json()
print(json.dumps(data, indent=2))