【发布时间】:2017-03-23 16:23:36
【问题描述】:
问题: 从rest api GET中存储的值创建json数据结构
问题: 动态创建嵌套数据结构
用于检索值的 Python 代码:
url = 'https://ciscoprime.com/webacs/api/v2/data/ClientDetails.json?.nocount=True&.maxResults=2&.firstResult=0&.full=true'
get_response = requests.get(url, verify=False)
response = json.dumps(get_response.json())
resp = json.loads(response)
#variable = resp['queryResponse']['entity'][i]['clientDetailsDTO']['']
i = 0
clients = {}
while i < len(resp['queryResponse']['entity']):
clientID = resp.get("queryResponse", {}).get("entity", {})[i].get("clientDetailsDTO", {}).get("@id")
associationTime = resp.get("queryResponse", {}).get("entity", {})[i].get("clientDetailsDTO", {}).get("associationTime")
clientInterface = resp.get("queryResponse", {}).get("entity", {})[i].get("clientDetailsDTO", {}).get("clientInterface")
connectionType = resp.get("queryResponse", {}).get("entity", {})[i].get("clientDetailsDTO", {}).get("connectionType")
deviceIpAddress = resp.get("queryResponse", {}).get("entity", {})[i].get("clientDetailsDTO", {}).get("deviceIpAddress")
deviceName = resp.get("queryResponse", {}).get("entity", {})[i].get("clientDetailsDTO", {}).get("deviceName")
firstSeenTime = resp.get("queryResponse", {}).get("entity", {})[i].get("clientDetailsDTO", {}).get("firstSeenTime")
ifIndex = resp.get("queryResponse", {}).get("entity", {})[i].get("clientDetailsDTO", {}).get("ifIndex")
ipAddress = resp.get("queryResponse", {}).get("entity", {})[i].get("clientDetailsDTO", {}).get("ipAddress")
macAddress = resp.get("queryResponse", {}).get("entity", {})[i].get("clientDetailsDTO", {}).get("macAddress")
updateTime = resp.get("queryResponse", {}).get("entity", {})[i].get("clientDetailsDTO", {}).get("updateTime")
vendor = resp.get("queryResponse", {}).get("entity", {})[i].get("clientDetailsDTO", {}).get("vendor")
vlan = resp.get("queryResponse", {}).get("entity", {})[i].get("clientDetailsDTO", {}).get("vlan")
vlanName = resp.get("queryResponse", {}).get("entity", {})[i].get("clientDetailsDTO", {}).get("vlanName")
#print 'ClientID: {0}, associationTime: {1}, vlanName: {2}'.format(clientID, associationTime, vlanName)
i += 1
GET 格式为 json
{"queryResponse": {"@responseType": "listEntityInstances",
"@count": 192077, "@last": 1, "@rootUrl": "https://ciscopri
me.com/webacs/api/v2/data", "@requestUrl": "https://c
iscoprime.com/webacs/api/v2/data/ClientDetails?.nocou
nt=True&.maxResults=2&.firstResult=0&.full=true
", "entity": [{"clientDetailsDTO": {"apName": "nzcfd-rotoof
f-ap2", "deviceName": "cntl-1", "updateTime":
1490290426910, "protocol": "DOT11N2_4GNZ", "clientInterfac
e": "management", "macAddress": "FF:FF:FF:FF:FF:FF", "ccxLS
Version": "V1", "policyTypeStatus": "NOMETHOD", "wgbMacAddr
ess": "00:00:00:00:00:00", "wepState": "DISABLED", "ccxVers
ion": "UNSUPPORTED", "speed": "UNKNOWN", "@displayName": "4
110106", "hreapLocallyAuthenticated": "2", "ssid": "Gue
st", "clientAaaOverrideAclApplied": "NA", "ccxVSVersion": "
V1", "securityPolicyStatus": "FAILED", "authenticationAlgor
ithm": "OPENSYSTEM", "connectionType": "LIGHTWEIGHTWIRELESS
", "postureStatus": "UNKNOWN", "deviceType": "none", "locat
ion": "Root Area", "wiredClientType": "NA", "status": "ASSO
CIATED", "firstSeenTime": 1487771895967, "wgbStatus": "REGU
LARCLIENT", "vendor": "Samsung Electronics", "vlan": "10",
"ccxMSVersion": "V1", "nacState": "ACCESS", "webSecurity":
"DISABLED", "policyType": "NA", "associationTime": 14881782
85705, "ifIndex": 0, "apIpAddress": {"address": "1.2.2.2"}, "@id": 4110106, "eapType": "UNNOWN", "mobilityStatus"
: "EXPORTANCHORED", "ipType": "IPV4", "ipAddress": {"addres
s": "10.2.2.2"}, "apSlotId": 0, "encryptionCypher": "
NONE", "apMacAddress": "FF:FF:FF:FF:FF:FF", "ccxFSVersion":
"V1", "clientApMode": "UNKNOWN", "deviceIpAddress": {"addr
ess": "172.2.2.2"}, "clientAclApplied": "NA"}, "@url": "
https://ciscoprime.com/webacs/api/v2/data/ClientDetai
ls/4110106", "@type": "ClientDetails", "@dtoType": "clientD
etailsDTO"}
所需的数据结构输出。
clientID : value
{
associationTime : value
clientInterface : value
etc...
}
clientID : value
{
associationTime : value
clientInterface : value
etc...
}
etc....
【问题讨论】:
-
您需要提供API GET请求的json返回数据的格式。
-
我已经用请求的信息更新了问题
-
您想要的数据结构输出不是 JSON。
-
我已删除该要求,因为它与此无关。 @SatishGarg
-
您拥有的 json:clientID: value { stuff} 无效。所以你不能那样做。但是您可以将实际的客户端 ID 作为字典对象的键。