且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Flask JSONify在新行上打印结果

更新时间:2023-12-03 17:45:40

摘要

  • 答案:< br>
  • 替代:JSONView Chrome扩展程序
  • Answer: <br>
  • Alternative: JSONView Chrome Extension

唯一给我好的结果的是< br> :

The only one that give me good results was <br>:

示例

from flask import Flask, jsonify

app = Flask(__name__)

tasks = [
    {
        '<br>id': 1,
        'title': u'Buy groceries',
        'description': u'Milk, Cheese, Pizza, Fruit, Tylenol', 
        'done': False
    },
    {
        '<br>id': 2,
        'title': u'Learn Python',
        'description': u'Need to find a good Python tutorial on the web', 
        'done': False
    }
]

@app.route('/todo/api/v1.0/tasks', methods=['GET'])
def get_tasks():
    return jsonify({'tasks': tasks})

if __name__ == '__main__':
    app.run(debug=True)

在您的浏览器中,< br> 字符将被呈现为html并重新生成新行.

In your browser the <br> character will be rendered as html and reproduce a new line.

结果:
在json中创建"新行

Jsonify无法帮助您,因为它将值(整数,布尔值,浮点数等)作为字符串,并且避免使用特殊字符,例如 \ n \ t

Jsonify can't help you because it takes the values (integer,boolean,float, etc) as a string and avoid special characters like \n, \t, etc

最后,如果您只想以一种精美的方式在浏览器中可视化json文件,则可以使用JSONView,这是一个Chrome扩展程序,可以像这样更易于理解的方式呈现Json文件.

Finally, if you just want a fancy way to visualize json files in your browser, you could use JSONView, is a Chrome extension that render Json files in a more understandable way, like this.

使用JSONView渲染