日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

【Flask项目2】定制统一的JSON返回格式(6)

發布時間:2025/3/21 javascript 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Flask项目2】定制统一的JSON返回格式(6) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

comment—utils—output.py

from flask import make_response, current_app from flask_restful.utils import PY3 from json import dumps# todo 將字典格式的響應數據轉化為json格式的響應數據 def output_json(data, code, headers=None):"""Makes a Flask response with a JSON encoded body"""#todo 此處添加自己定義的json格式規則,把返回給前端的數據做一個封裝,以便于前端可以使用統一的規則解析數據if 'message' not in data:data = {# 'message':'OK','code': 200, # 自動的將狀態200封裝到json中'data': data}settings = current_app.config.get('RESTFUL_JSON', {})# If we're in debug mode, and the indent is not set, we set it to a# reasonable value here. Note that this won't override any existing value# that was set. We also set the "sort_keys" value.if current_app.debug:settings.setdefault('indent', 4)settings.setdefault('sort_keys', not PY3)# always end the json dumps with a new line# see https://github.com/mitsuhiko/flask/pull/1262#todo 將字典轉化為jsondumped = dumps(data, **settings) + "\n"resp = make_response(dumped, code)resp.headers.extend(headers or {})return resp

使用:在資源視圖中(創建藍圖的py文件中)定義

user_api.representation('aplication/json')(output_json)

總結

以上是生活随笔為你收集整理的【Flask项目2】定制统一的JSON返回格式(6)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。