形成httplib响应(json)

时间:2015-04-30 13:18:39

标签: python json httplib

经过一些研究后,我能够通过网络设备从RESTful API获取数据:

if sys.version_info >= (2,7,9):
    import ssl
    conn = httplib.HTTPSConnection('192.168.158.136', 443, context=ssl._create_unverified_context())
else:
    conn = httplib.HTTPSConnection('192.168.158.136', 443)

headers = {"Authorization"  : "Basic %s" % base64.b64encode('user:pass'),
           "Content-Type"   : "application/json"}

url="https://192.168.158.136/wapi/v1.2/network?_return_fields%2b=extattrs"

conn.request('GET', url, headers=headers)
response = conn.getresponse()

print response.read()
conn.close()

这会打印出我需要的对象的json格式列表:

[
    {
        "_ref": "network/ZG5zLm5ldHdvcmskMTAuMC4wLjAvMjQvMA:10.0.0.0/24/default", 
        "extattrs": {
            "Location": {
                "value": "NAU"
            }
        }, 
        "network": "10.0.0.0/24", 
        "network_view": "default"
    }, 
    {
        "_ref": "network/ZG5zLm5ldHdvcmskMTAuMS4xLjAvMjQvMA:10.1.1.0/24/default", 
        "extattrs": {
            "Location": {
                "value": "BTN"
            }
        }, 
        "network": "10.1.1.0/24", 
        "network_view": "default"
    }, 
    {
        "_ref": "network/ZG5zLm5ldHdvcmskMTAuMi4yLjAvMjQvMA:10.2.2.0/24/default", 
        "extattrs": {
            "Location": {
                "value": "TRT"
            }
        }, 
        "network": "10.2.2.0/24", 
        "network_view": "default"
    }, 
    {
        "_ref": "network/ZG5zLm5ldHdvcmskMTkyLjE2OC4wLjAvMTYvMA:192.168.0.0/16/default", 
        "extattrs": {
            "Location": {
                "value": "MCW"
            }
        }, 
        "network": "192.168.0.0/16", 
        "network_view": "default"
    }
]

现在我需要"不知何故"将其格式化为仅包含" network"以及extattrs"位置"的内容(并且只有位置,还有其他人,但我只需要位置),格式为:

网络位置,即

10.0.0.0/24 NAU
10.1.1.0/24 BTN
10.2.2.0/24 TRT
192.168.0.0/16 MCW

我努力完成这项工作,即重新格式化我从查询中获取的JSON代码。

2 个答案:

答案 0 :(得分:0)

您只需使用json模块将JSON数组解析为dict。

import json

# Your code here

net_loc = []
resp_d = json.loads(response.read())
for obj in resp_d:
    net_loc.append(obj["network"] + " " + obj["extattrs"]["Location"]["value"])

net_loc列表将包含您想要的内容。

答案 1 :(得分:0)

发布map方法风险很大,因为人们似乎不喜欢map,但我会尝试一下:

if sys.version_info >= (2,7,9):
    import ssl
    conn = httplib.HTTPSConnection('192.168.158.136', 443, context=ssl._create_unverified_context())
else:
    conn = httplib.HTTPSConnection('192.168.158.136', 443)

import json
headers = {"Authorization"  : "Basic %s" % base64.b64encode('user:pass'),
           "Content-Type"   : "application/json"}

url="https://192.168.158.136/wapi/v1.2/network?_return_fields%2b=extattrs"

conn.request('GET', url, headers=headers)
response = conn.getresponse()

data = json.loads(response.read())
conn.close()

#variant 1. "map" approach
networks = map(lambda x: "%s %s" % (x["network"], x["extattrs"]["Location"]["value"]), data)

print("\n".join(networks))

#variant 2. "for" approach
networks = []
for item in data:
  networks.append("%s %s" % (item["network"], item["extattrs"]["Location"])["value"]))

print("\n".join(networks))