因此,我正在使用Cisco Prime基础设施的API,而我已经走到尽头了。我正在编写的脚本应该从其getReport操作中获取报告,对其进行解码(不确定是否正确),然后将其放入Pandas DataFrame中。问题是,周围有垃圾。这是响应json的示例:
{
"mgmtResponse" : {
"@requestUrl" : "https://localhost/webacs/api/v4/op/reportService/getReport?reportTitle=MyReport",
"@responseType" : "operation",
"@rootUrl" : "https://localhost/webacs/api/v4/op",
"reportDataDTO" : [ {
"childReports" : {
"childReport" : [ ]
},
"dataRows" : {
"dataRow" : [ {
"entries" : {
"entry" : [ {
"attributeName" : "String value",
"dataValue" : "String value",
"displayName" : "String value"
}, {
"attributeName" : "Another string value",
"dataValue" : "Another string value",
"displayName" : "Another string value"
} ]
}
}, {
"entries" : {
"entry" : [ {
"attributeName" : "String value",
"dataValue" : "String value",
"displayName" : "String value"
}, {
"attributeName" : "Another string value",
"dataValue" : "Another string value",
"displayName" : "Another string value"
} ]
}
} ]
},
"descriptorName" : "String value",
"pageCount" : 15,
"pageIndex" : 15,
"reportDate" : "String value",
"reportName" : "String value",
"reportTitle" : "String value",
"reportUrl" : "String value"
} ]
}
}
我希望脚本仅使用嵌套在“ dataRows”下的信息,但是我不知道该怎么做。到目前为止,我有这个:
response = rq.get(url, auth=(cpi_user,cpi_password), verify=False, timeout = 300)
print(response.status_code)
if (response.status_code == rq.codes.ok):
responseJSON = response.json()
rogue_ap_flatten = json_normalize(responseJSON)
print (rogue_ap_flatten)
rogues = pd.DataFrame(rogue_ap_flatten)
print(rogues.head(50))
return rogues
我得到的回报是:
mgmtResponse.@requestUrl ... mgmtResponse.reportDataDTO
0 https://prime/webacs/api/v4/op/reportS... ... [{'childReports': {'childReport': []}, 'dataRo...
[1 rows x 4 columns]
我已经尝试过仅使用请求中的.text方法,也尝试过使用另一个json展平库(json_flatten),并且可以选择排除某些键,并且我正在考虑以某种方式在python中使用sed。它不需要为其他报告工作,只需一个,所以我有一些余地可以指定任何特定的键或其他。你们将如何解决这个问题?
您应该可以dataRows
使用以下内容来了解这些东西:
import json
data = {<your data>}
print(json.dumps(result['mgmtResponse']['reportDataDTO'][0]['dataRows'], indent=4))
输出量
{
"dataRow": [
{
"entries": {
"entry": [
{
"attributeName": "String value",
"dataValue": "String value",
"displayName": "String value"
},
{
"attributeName": "Another string value",
"dataValue": "Another string value",
"displayName": "Another string value"
}
]
}
},
{
"entries": {
"entry": [
{
"attributeName": "String value",
"dataValue": "String value",
"displayName": "String value"
},
{
"attributeName": "Another string value",
"dataValue": "Another string value",
"displayName": "Another string value"
}
]
}
}
]
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句