Flask中jsonify和json.dumps用法以及区别(简单案例)
json
适用于现代 C++ 的 JSON。
项目地址:https://gitcode.com/gh_mirrors/js/json
免费下载资源
·
环境:python3.6, Flask1.0.3
flask提供了jsonify函数供用户处理返回的序列化json数据,
而python自带的json库中也有dumps方法可以序列化json对象.
其二者的区别,写个简单的案例实测一下便见分晓。
from flask import Flask
from flask import jsonify
import json
app=Flask(__name__)
app.config['JSON_AS_ASCII'] = False
# 随便定义个json字典
dic={"a":1,"b":2,"c":"你好"}
@app.route('/jsonify')
def jsonifys():
# Content-Type: application/json
return jsonify(dic)
@app.route('/jsondumps')
def jsondumps():
# Content-Type: text/html; charset=utf-8
return json.dumps(dic,ensure_ascii=False)
if __name__ == '__main__':
app.run(debug=True)
1.我们先访问 http://127.0.0.1:5000/jsonify 地址查看jsonify返回的json内容如下
检查页面属性看Response内容:
2.再访问另一个地址 http://127.0.0.1:5000/jsondumps 查看返回的json内容如下
检查页面属性看Response内容:
使用jsonify时响应的Content-Type字段值为application/json,
而使用json.dumps时该字段值为text/html。
ps:
使用jsonify方法是需要添加一句 app.config['JSON_AS_ASCII'] = False
而json.dumps方法需要添加参数 ensure_ascii=False
这样做可以避免显示中文乱码。
GitHub 加速计划 / js / json
18
5
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:3 个月前 )
f06604fc
* :page_facing_up: bump the copyright years
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
* :page_facing_up: bump the copyright years
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
* :page_facing_up: bump the copyright years
Signed-off-by: Niels Lohmann <niels.lohmann@gmail.com>
---------
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
Signed-off-by: Niels Lohmann <niels.lohmann@gmail.com> 2 天前
d23291ba
* add a ci step for Json_Diagnostic_Positions
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* Update ci.cmake to address review comments
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* address review comment
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix typo in the comment
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix typos in ci.cmake
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* invoke the new ci step from ubuntu.yml
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* issue4561 - use diagnostic positions for exceptions
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix ci_test_documentation check
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* address review comments
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix ci check failures for unit-diagnostic-postions.cpp
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* improvements based on review comments
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix const correctness string
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* further refinements based on reviews
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* add one more test case for full coverage
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* ci check fix - add const
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* add unit tests for json_diagnostic_postions only
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix ci_test_diagnostics
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix ci_test_build_documentation check
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
---------
Signed-off-by: Harinath Nampally <harinath922@gmail.com> 3 天前
更多推荐
已为社区贡献7条内容
所有评论(0)