【问题标题】:Extracting relevant keys from Immutable dict in python flask api从 python flask api 中的不可变字典中提取相关键
【发布时间】:2018-04-19 11:32:33
【问题描述】:

put req screenshot您好我目前正在尝试在python中解析以下post请求

-------------------------------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 提取为字典,以便我可以访问版本值、子名称值和 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--

我不知道该怎么走,干杯。

【问题讨论】:

标签: python json flask


【解决方案1】:

您需要将您发布的 json 数据转换为字典:

json_dict = request.get_json()

然后您需要对其进行解析以获得正确的信息:

UUID = json_dict['Alert'][0]['Event']['SensorUUID']

编辑: 最小的工作示例:

卷曲

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!

【讨论】:

  • 感谢您的评论 json_dict = request.get_json() 返回无类型
  • 那么您没有正确发布表单。你能显示发布 json 的代码吗?
  • 您好,感谢您的帮助。我正在使用一个名为 kiwiserver 的软件,它会自动发送定期发布请求。这是标题 { "VERSION": "HTTP/1.1", "CONNECTION": "close", "ACCEPT-ENCODING": "gzip", "ACCEPT": "application/json, application/xml, text/json, text/x-json, text/javascript, text/xml", "USER-AGENT": "RestSharp/105.2.3.0", "CONTENT-TYPE": "multipart/form-data;boundary=------ -----------------------28947758029299", "内容长度": "1780" }
  • 将标题中的CONTENT-TYPE 更改为application/json
  • 我已经编辑在 putreq 中包含请求的屏幕截图。
【解决方案2】:

我使用以下代码解决了这个问题。通过使请求正文成为字符串并对其进行解析。感谢您的帮助。

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)

【讨论】:

    猜你喜欢
    • 2012-08-14
    • 2016-05-15
    • 1970-01-01
    • 2021-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多