もしあなたがする必要があるいくつかの理由でオーバーライドflask.jsonify
(例えばカスタムエンコーダを追加する)あなたが言及したセキュリティ修正@phpmycoder実装し、以下の方法で行うことができます。
from json import dumps
from flask import make_response
def jsonify(status=200, indent=4, sort_keys=True, **kwargs):
response = make_response(dumps(dict(**kwargs), indent=indent, sort_keys=sort_keys))
response.headers['Content-Type'] = 'application/json; charset=utf-8'
response.headers['mimetype'] = 'application/json'
response.status_code = status
return response
app.route('/<major>/')
def major_res(major):
course = client.db.course_col.find({"major": (major.encode("utf8", "ignore").upper())})
return jsonify(**course)
app.route('/test/')
def test():
return jsonify(indent=2, sort_keys=False, result="This is just a test")
応答:
を
{
"course": "CSCI052",
"description": "Fundamentals of Computer Science. A solid foundation in functional programming, procedural and data abstraction, recursion and problem-solving. Applications to key areas of computer science, including algorithms and complexity, computer architecture and organization, programming languages, finite automata and computability. This course serves the same role as HM 60 as a prerequisite for upper-division computer science courses at any of the Claremont Colleges. Prerequisite: 51.",
"instructor": "Bull, Everett L.,, Jr.",
"name": " Fundamentals of Computer Science",
"number": 52,
"school": "PO"
}
は、例えば私のother answerこの使用 `` json.dimps` indent`引数にはカスタムJSONエンコーダ
を使用して参照してください、しかし、あなたはすでにそれを持っています。コンマと ':'の後ろにスペースがあるので、これは縮小されたJSONではありません。だから、どのようにこの出力を得る(ブラウザ、ログ/プリント、wget)?おそらく 'return response(、mimetype = 'application/json')'や 'flask.jsonofy'と' indent'引数を使用する必要があります。 –
tbicr