j用反斜杠将响应数据唯一化

2024-10-04 09:22:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个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)

Tags: toidjsondictionaryreturnapplication格式business