且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

json.dumps 与 flask.jsonify

更新时间:2023-09-11 21:35:52

flask 中的 jsonify() 函数返回一个 flask.Response() 对象,该对象已经有了与 json 响应一起使用的适当的内容类型标头application/json".而 json.dumps() 方法只会返回一个编码字符串,这需要手动添加 MIME 类型标头.

The jsonify() function in flask returns a flask.Response() object that already has the appropriate content-type header 'application/json' for use with json responses. Whereas, the json.dumps() method will just return an encoded string, which would require manually adding the MIME type header.

查看更多关于 jsonify() 函数的信息这里 供完整参考.

See more about the jsonify() function here for full reference.

另外,我注意到 jsonify() 处理 kwargs 或字典,而 json.dumps() 还支持列表和其他.

Also, I've noticed that jsonify() handles kwargs or dictionaries, while json.dumps() additionally supports lists and others.