python flask解析json数据不完整的解决方法
作者:钟摆人 时间:2021-07-12 01:45:00
当使用Python的flask框架来开发网站后台,解析前端Post来的数据,通常都会使用request.form来获取前端传过来的数据,但是如果传过来的数据比较复杂,其中右array,而且array的元素不是单个的数字或者字符串的时候,就会出现解析不到数据的情况,比如使用下面的js代码向python flask传递数据
$.ajax({
"url":"/test",
"method":"post",
"data":{
"test":[
{"test_dict":"1"},
{"test_dict":"2"},
{"test_dict":"3"},
]
}
}
)
当我们使用flask的request.form获取前端的数据时,发现获取到的数据是这样的:
ImmutableMultiDict([('test', 'test_dict'), ('test', 'test_dict'), ('test', 'test_dict')])
???我的Post数据呢?给我post到哪里去了???
这里我就去网上查解决办法,但是网上哪些删么使用reqeust.form.getlist()方法好像都对我无效,但是又找不到其他的解决方案?怎么办?
规范一下自己的请求,在前端请求的时候设置一个Json的请求头,在flask框架钟直接使用json.loads()方法解析reqeust.get_data(as_text=True),就可以解析到完整的post参数了!
前端:
$.ajax({
"url":"/test",
"method":"post",
"headers":{"Content-Type": "application/json;charset=utf-8"},//这一句很重要!!!
"data":{
"test":[
{"test_dict":"1"},
{"test_dict":"2"},
{"test_dict":"3"},
]
}
}
)
python代码:
@app.route("/test",methods=["GET","POST"])
def test():
print(json.loads(request.get_data(as_text=True)))
return ""
然后看看后台打印的信息:
* Serving Flask app "test_flask.py"
* Environment: development
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
{'test': [{'test_dict': '1'}, {'test_dict': '2'}, {'test_dict': '3'}]}
127.0.0.1 - - [25/May/2019 22:43:08] "POST /test HTTP/1.1" 200 -
问题解决,可以解析到完整的json数据啦!
来源:http://www.cnblogs.com/zhongbr/p/python_flask.html
标签:flask,解析,json
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
不管你的Python报什么错,用这个模块就能正常运行
2023-08-29 14:42:43
![](https://img.aspxhome.com/file/2023/3/62863_0s.jpg)
python解析html提取数据,并生成word文档实例解析
2023-10-19 13:50:38
Opera浏览器简介
2009-02-05 20:56:00
Python操作json数据的一个简单例子
2022-10-23 17:09:37
python实现复制整个目录的方法
2023-04-08 18:14:47
Go语言struct要使用 tags的原因解析
2023-08-31 09:25:19
![](https://img.aspxhome.com/file/2023/6/93666_0s.jpg)
解决PyCharm不在run输出运行结果而不是再Console里输出的问题
2023-05-01 17:47:17
![](https://img.aspxhome.com/file/2023/2/102932_0s.png)
Python使用read_csv读数据遇到分隔符问题的2种解决方式
2022-01-13 13:30:47
![](https://img.aspxhome.com/file/2023/7/103507_0s.png)
Python 做曲线拟合和求积分的方法
2021-03-03 01:46:07
python实战之实现excel读取、统计、写入的示例讲解
2022-02-12 17:42:15
Python机器学习入门(五)之Python算法审查
2021-10-15 09:27:48
![](https://img.aspxhome.com/file/2023/8/101118_0s.jpg)
PHP扩展之kafka安装应用案例详解
2023-09-06 09:53:43
python调试模块ipdb详解
2021-11-05 19:59:01
![](https://img.aspxhome.com/file/2023/0/79790_0s.png)
python pyheatmap包绘制热力图
2021-02-18 21:35:46
正则表达式字面量在ECMAScript5中的变化
2012-04-26 16:23:16
Oracle 的入门心得 强烈推荐
2009-05-24 19:55:00
新功能的帮助与破坏
2010-01-17 10:15:00
![](https://img.aspxhome.com/file/UploadPic/20101/17/bai_photo-14s.png)
在tensorflow中实现屏蔽输出的log信息
2023-02-27 17:41:21
Python爬虫爬取微博热搜保存为 Markdown 文件的源码
2023-03-03 23:30:29
![](https://img.aspxhome.com/file/2023/7/67297_0s.jpg)
详解Python中的数据精度问题
2022-08-17 13:36:22
![](https://img.aspxhome.com/file/2023/9/101089_0s.png)