python - 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"
}
所以我想要“设备”的价值。
解决方案
如果您的请求的内容类型是application/json
,request.get_json()
将返回一个与您的 JSON 结构相对应的字典。(对于其他内容类型,它将返回None
。)
这意味着您可以通过以下方式访问设备的值
content = request.get_json()
device = content["device"]
推荐阅读
- java - 无法读取未定义的属性“种类”:TypeError:无法读取未定义的属性“种类”
- java - Spring Boot OAuth 始终重定向到 HTTP (IBM Cloud CF + Spring Boot 2)
- c# - MVVM 将组合框内容绑定到字典中的列表
- php - 我如何从数据库中删除数据,除了最后 24 小时 UNIX 时间格式
- javascript - js window.matchMedia 返回错误的结果
- python - Django - 通过 Ajax 发布 jQuery 字典在 views.py 中打印 None
- java - Apache-POI 在 Excel 中设置值,但另一个单元格的公式无法使用该值,直到我在处理条中手动按 enter
- angular - Angular:如何在动态组件中使用共享模块?
- php - 按自定义字段对管理列进行排序未按预期工作
- soap - 使用 Sabre soap api 访问 pnrs 列表并从队列中删除