我在该网站上做了很多阅读,内容涉及从Python到API的发布请求。尽管提出了在Python中使用json
库的所有建议,但我仍然无法完全解决。
我当前的困境是我需要进行API调用,获取某些字段并将其发布到另一个API。
我从最初的API请求中收到的信息示例:
{
"metadata": {
"configurationVersions": [
3
],
"clusterVersion": "1.174.168.20190814-173650"
},
"id": "5c1547a6-61ca-4dc3-8971-ec8d2f542592",
"name": "Registration",
"enabled": false,
"dataType": "STRING",
"dataSources": [
{
"enabled": true,
"source": "POST_PARAMETER",
"valueProcessing": {
"splitAt": "",
"trim": false
},
"parameterName": "f=register",
"scope": {
"tagOfProcessGroup": "Production"
}
}
],
"normalization": "ORIGINAL",
"aggregation": "FIRST",
"confidential": true,
"skipPersonalDataMasking": true
}
此调用后,我通过以下方式提取数据:
def ReqOutput(output):
x=""
out = ()
inReq = ["name","enabled","dataType","dataSources","normalization","aggregation","confidential","skipPersonalDataMasking"]
for i in output.items():
for item in inReq:
if item in i:
x = x + str(i)
out=out + i
return json.dumps(out)
按照其他线程的建议,我曾经使用json.dumps
方法将python元组转换为JSON。但是,我觉得它没有按预期工作
json.dumps
之前的输出:
'name', 'Registration', 'enabled', False, 'dataType', 'STRING', 'dataSources', [{'enabled': True, 'source': 'POST_PARAMETER', 'valueProcessing': {'splitAt': '', 'trim': False}, 'parameterName': 'f=register', 'scope': {'tagOfProcessGroup': 'Production'}}], 'normalization', 'ORIGINAL', 'aggregation', 'FIRST', 'confidential', True, 'skipPersonalDataMasking', True)
发布json.dumps
输出:
["name", "Registration", "enabled", false, "dataType", "STRING", "dataSources", [{"enabled": true, "source": "POST_PARAMETER", "valueProcessing": {"splitAt": "", "trim": false}, "parameterName": "f=register", "scope": {"tagOfProcessGroup": "Production"}}], "normalization", "ORIGINAL", "aggregation", "FIRST", "confidential", true, "skipPersonalDataMasking", true]
然后我尝试使用以下方法将此发布到另一个API:
def PostRequest (data):
postURL = "XXXX"
headers = {'Content-type': 'application/json'}
r = requests.post(postURL,data = data,headers = headers)
print(r.text)
最后我遇到错误的地方:
{"error":{"code":400,"message":"Could not map JSON at '' near line 1 column 1"}}
答案 0 :(得分:0)
尝试摆脱Getuser(type)
If Type = "P" Then
DataTable.ImportSheet LocalFicheiros & "LOGIN\input.xlsx", "Particular", "Global"
Else
DataTable.ImportSheet LocalFicheiros & "LOGIN\input.xlsx", "Business", "Global"
End If
循环以支持字典理解:
for
这更具可读性,将始终为您提供def ReqOutput(output):
inReq = ["name","enabled","dataType","dataSources","normalization","aggregation","confidential","skipPersonalDataMasking"]
out = {output: val for output, val in output.items() if output in inReq}
return json.dumps(out)
中dict
中具有属性output
的{{1}}。您的inReq
之所以看起来像是因为序列化JSON
会为您提供类似tuple
的结构。如果您想要的是Array
结构,则应该序列化类似Object
的对象。