put req screenshot您好我正在尝试在python中解析以下帖子请求
-------------------------------28947758029299
Content-Disposition: form-data; name="eventData"; filename="eventData.txt"
Content-Type: application/json; charset=utf-8
{
"Rule" : "sendSpots",
"Alert" : [
{
"Event" : {
"Version" : "1",
"EventUUID" : "fe1817b9-8557-4784-b64a-617c50fe27b8",
"Type" : "Vision",
"Subtype" : "Alarm",
"SensorUUID" : "!id:9298e8e1-feb0-48e3-9c44-11ee42672ac9!",
"Origin" : "DVS-BOX-PC",
"InstanceType" : "ParkingSpaceAnalyzer",
"InstanceName" : "ParkingSpotAnalyser",
"SubName" : "P01",
"FrameTime" : "2018-04-19T09:59:41.0970000Z",
"Parameters" : [
{
"Name" : "DURATION",
"Type" : "int",
"Value" : "3669869"
},
{
"Name" : "STREAM_RESOLUTION",
"Type" : "string",
"Value" : "1280,720"
},
{
"Name" : "GENERIC_XML",
"Type" : "string",
"Value" : "<EventData><ParkingState type=\"string\">occupied</ParkingState><ParkingDurationExceeded type=\"int\">1</ParkingDurationExceeded></EventData>"
},
{
"Name" : "REGION",
"Type" : "string",
"Value" : "638,613 694,377 792,373 695,614"
}
]
}
}
]
}
-------------------------------28947758029299--
我希望将Event提取为字典,以便我可以访问版本值,Subname值和SensorUUId。为了尝试提取此信息,我使用了代码
data = request.form
body = data['-------------------------------28947758029299\nContent-Disposition: form-data; name']
然而,这会返回以下类型unicode:
"eventData"; filename="eventData.txt"
Content-Type: application/json; charset=utf-8
{
"Rule" : "sendSpots",
"Alert" : [
{
"Event" : {
"Version" : "1",
"EventUUID" : "fe1817b9-8557-4784-b64a-617c50fe27b8",
"Type" : "Vision",
"Subtype" : "Alarm",
"SensorUUID" : "!id:9298e8e1-feb0-48e3-9c44-11ee42672ac9!",
"Origin" : "DVS-BOX-PC",
"InstanceType" : "ParkingSpaceAnalyzer",
"InstanceName" : "ParkingSpotAnalyser",
"SubName" : "P01",
"FrameTime" : "2018-04-19T09:59:41.0970000Z",
"Parameters" : [
{
"Name" : "DURATION",
"Type" : "int",
"Value" : "3669869"
},
{
"Name" : "STREAM_RESOLUTION",
"Type" : "string",
"Value" : "1280,720"
},
{
"Name" : "GENERIC_XML",
"Type" : "string",
"Value" : "<EventData><ParkingState type=\"string\">occupied</ParkingState><ParkingDurationExceeded type=\"int\">1</ParkingDurationExceeded></EventData>"
},
{
"Name" : "REGION",
"Type" : "string",
"Value" : "638,613 694,377 792,373 695,614"
}
]
}
}
]
}
-------------------------------28947758029299--
我不知道如何进一步欢呼。
答案 0 :(得分:0)
您需要将发布的json数据转换为字典:
json_dict = request.get_json()
然后您需要解析以获取正确的信息:
UUID = json_dict['Alert'][0]['Event']['SensorUUID']
编辑: 最小的工作范例:
CURL
curl -X POST -H "Content-Type: application/json" -d '{
"Rule" : "sendSpots",
"Alert" : [
{
"Event" : {
"Version" : "1",
"EventUUID" : "fe1817b9-8557-4784-b64a-617c50fe27b8",
"Type" : "Vision",
"Subtype" : "Alarm",
"SensorUUID" : "!id:9298e8e1-feb0-48e3-9c44-11ee42672ac9!",
"Origin" : "DVS-BOX-PC",
"InstanceType" : "ParkingSpaceAnalyzer",
"InstanceName" : "ParkingSpotAnalyser",
"SubName" : "P01",
"FrameTime" : "2018-04-19T09:59:41.0970000Z",
"Parameters" : [
{
"Name" : "DURATION",
"Type" : "int",
"Value" : "3669869"
},
{
"Name" : "STREAM_RESOLUTION",
"Type" : "string",
"Value" : "1280,720"
},
{
"Name" : "GENERIC_XML",
"Type" : "string",
"Value" : "<EventData><ParkingState type=\"string\">occupied</ParkingState><ParkingDurationExceeded type=\"int\">1</ParkingDurationExceeded></EventData>"
}' http://127.0.0.1:5000/
的Python:
from flask import Flask, render_template,request, render_template_string
app = Flask(__name__)
@app.route("/", methods=['GET', 'POST'])
def homepage_process():
if request.method == 'POST':
json_dict = request.get_json()
return json_dict['Alert'][0]['Event']['SensorUUID']
响应:
!id:9298e8e1-feb0-48e3-9c44-11ee42672ac9!
答案 1 :(得分:0)
我使用以下代码解决了这个问题。通过使请求主体成为一个字符串并解析它。谢谢你的帮助。
data = request.stream.read()
first_s = data.find('{')
last_s = data.rfind('}')
data = data[first_s:last_s+1]
data = json.loads(data)
data = dict(data)