本文介绍了防止Flask jsonify对数据进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次使用jsonify时,都会得到按字母顺序排序的JSON键.我不希望按键排序.我可以禁用在jsonify中完成的排序吗?

Each time I use jsonify, I get the JSON keys sorted alphabetically. I don't want the keys sorted. Can I disable the sorting done in jsonify?

from flask import request, jsonify

@app.route('/', methods=['POST'])
def index():
    json_dict = request.get_json()
    user_id = json_dict['user_id']
    permissions = json_dict['permissions']
    data = {'user_id': user_id, 'permissions': permissions}
    return jsonify(data)

推荐答案

是的,您可以使用config属性进行修改:

Yes, you can modify this using the config attribute:

app = Flask(__name__)
app.config['JSON_SORT_KEYS'] = False

但是,请注意,在文档中明确警告了这一点:

However, note that this is warned against explicitly in the documentation:

这篇关于防止Flask jsonify对数据进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 10:26