jsonify

在flask中通过响应,将json数据显示在网页中,并且将Content-Type该为application/json格式

1,第一种方法:

from flask import jsonify

@app.route("/index")
def index():
    data = {"name":"zeng", "age":23}
    return jsonify(data)

2,第二种方法:

import json

@app.route("/index")
def index():
    data = {"name":"zeng", "age":23}
    json_str = json.dumps(data)
    return json, 200, {"Content-Type": "application/json"}

,3,第三种方法:

import json

@app.route("/index")
def index():
    data = {"name":"zeng", "age":23}
    json_str = json.dumps(data)
    resp = make_response(json_str)
    resp.headers["Content-Type"] = "application/json"
    return resp

 

posted on 2018-12-12 17:07  zengsf  阅读(1001)  评论(0编辑  收藏  举报

导航