我有一个flaskapi,它以json格式发送响应
rep = {'application_id': 32657, 'business_rules_id': 20} # a python dictionary
rep_json = json.dumps(rep, cls=CustomizedEncoder) # converts to a json format string
return jsonify(rep_json), 200 . #return the flask response (with headers etc)
我可以看到烧瓶反应体的数据,反应是这样的:
b'"{\\"application_id\\": 32567, \\"business_rules_id\\": 20}"\n'
或者在邮递员尸体里
"{\"application_id\": 32567, \"business_rules_id\": 20}
我应该得到一个JSON格式的响应吗(没有反斜杠)?我猜原因是json.dumps将字符串转储到json一次,然后jsonify再次转储它,这会导致双引号转义
我之所以需要运行以下命令,是因为我需要一个jsonify可能不支持的定制编码器
rep_json = json.dumps(rep, cls=CustomizedEncoder)
我的另一个解决方案是转储,然后加载,但这使它看起来是多余的。在返回Flask响应时,是否有不同的方法来使用自定义编码器
这是我尝试过的另一种方式,但看起来很奇怪
rep = {'application_id': 32657, 'business_rules_id': 20} # a python dictionary
rep_json = json.dumps(rep, cls=CustomizedEncoder) # converts to a json format string
return jsonify(json.loads(rep_json)), 200 . #return the flask response (with headers etc)
您可以将应用程序配置为使用带有
app.json_encoder = CustomizedEncoder
的客户编码器https://kite.com/python/docs/flask.app.Flask.json_encoder
相关问题 更多 >
编程相关推荐