首页 > 解决方案 > flask python - 提取在 post 方法中发送的 json 数据

问题描述

我需要提取作为 json 发送的对象我有这段代码,它给我带来了名为 content 的对象中的 json

  @app.route('/AddMessage', methods = ['POST'])
  def AddMessage():
    print (request.is_json)
    content = request.get_json()
    print (content)
    return 'JSON posted'

我想实现数据的属性。

我发送的数据是:

   {
     "device": "TemperatureSensor",
      "value": "20",
     "timestamp": "25/01/2017 10:10:05"
   }

所以我想要“设备”的价值。

标签: pythonflaskpost

解决方案


如果您的请求的内容类型是application/jsonrequest.get_json()将返回一个与您的 JSON 结构相对应的字典。(对于其他内容类型,它将返回None。)

这意味着您可以通过以下方式访问设备的值

content = request.get_json()
device = content["device"]

推荐阅读